Git Product home page Git Product logo

lab-props-and-state's Introduction

logo_ironhack_blue 7

React | Props & State

Introduction

You just learned the basics of React, how important are props and state in a React App, and how useful are for manage data in our application

In this exercsie wi will use Create React App and we will handle a Counter and a simple Form

Requirements

  • Fork this repo
  • Clone this repo

Submission

  • Upon completion, run the following commands:

    git add .
    git commit -m "your doubts"
    git push origin master
    
  • Create Pull Request so your TAs can check up your work.

Instructions

We will using the Create React App CLI, but don't worry about it, we already started the project ๐Ÿ˜‰

DON'T FORGET, BY DEFAULT CRA INITIALIZE GIT IN THE PROJECT

Install the dependencies

$ npm install
or
$ yarn install

Structure of our app

  • Create the components folder inside the src folder.
  • Inside the components folder create two new components, Counter.js and Form.js
lab-props-and-state
โ”‚   node_modules
โ”‚   public
โ””โ”€โ”€โ”€ src
    โ””โ”€โ”€โ”€ components
    โ”‚   โ””โ”€โ”€โ”€ Counter.js
    โ”‚   โ””โ”€โ”€โ”€ Form.js
    โ””โ”€โ”€โ”€ App.css
    โ””โ”€โ”€โ”€ App.js
    โ””โ”€โ”€โ”€ App.test.js
    โ””โ”€โ”€โ”€ index.css
    โ””โ”€โ”€โ”€ index.js
    โ””โ”€โ”€โ”€ logo.svg
    โ””โ”€โ”€โ”€ serviceWorker.js

Iteration 1 - Presentational components

Let's start with the basics: Let's put some pretty and dumb HTML in our presentational components.

Counter.js

  • This component should be a function component.

  • Should return only HTML:

    • Return an <h2>, the value of the count must be in this field.

    • Return an increment <button>, this button should increment the count in 1.

    • Return a decrement <button>, this button should decrement the count in 1.

  • Because our component is dumb and it's only for presentation purpose, should recieve by props the logic for increment and decrement the count, and the count itself.

Form.js

  • This component should be a function component.

  • Should return only HTML:

    • Return 3 <input>, don't forget to give them a name, respectively.

    • Return 3 <p>, each p should display the inputs value, respectively.

Iteration 2 - The logic

Remember, there should always be only one component that manages the state of our application and through props can communicate the information to the components that are designed for that.

So, let's do this. We will have all our logic in the App.js.

  • Our App.js should have an state, with the following values:
state = {
  count: 0,
  form: {
    name: '',
    surname: '',
    age: ''
  }
}
  • Should be a function that can manage the increment of or counter, called increment.

  • Should have a function that can manage the decrement of our counter, called decrement.

  • Should have a function that can manage the values of our inputs, called handleChange.

Remember which method we use to update our state

Iteration 3 - Magic!

In the App.js, you should return the <Counter/> component and the <Form/> component. Don't forget to pass the necessary props to these components.

It should looks like this:

...

render() {
    <div>
        <Counter {/* Here should be magic! (props) */} />
        <Form {/* Here should be magic! (props) */} />
    </div>
}

...

Iteration 4 - Styles

Don't forget the CSS, add some styles to our application to make it look better.

Happy coding! โค๏ธ

By: DiuriVJ :shipit:

lab-props-and-state's People

Contributors

felipe-parra avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.