Git Product home page Git Product logo

week_05_react_state_exercise_1's Introduction

You Do: Implement State (20 minutes)

Let's implement state in our Blog by making body a mutable value.

Remember to switch the running application!

Note: In React state just represents the state of data on our page. Something saved to state in React is not automatically saved to a database, or to local storage. state is just what's currently on the page. If you refresh the page then all state is lost, and refreshed with the page.

  1. Set an initial state inside a constructor() method for our Post component. The state attribute should be called body. Set the value of the body key to be the body prop that's passed into the component.
  2. Modify Post's render method so that it uses the body from state, not props.
  3. Create a changeBody method inside Post that updates body based on a user's input.
  • You should use setState somewhere in this method.
  • How can you get user input? Keep it simple and start with prompt.
  1. Add a button to Post's render method that triggers changeBody.

Solution

Your solution should look as follows:

Solution for Project

Bonus I

Use a form to take in user input.

  • The blog post's body should be updated dynamically when the user types in an input field.
  • One option is to keep track of what the new input is going to be by triggering a method using onChange on the <input>.
  • Another option is to pass an event object to the onSubmit method and traverse the DOM from the event's target (for example, e.target) to find the <input> value.
  • Note: You can leave the button from above to give the user options!

Bonus Solution

Your solution should look as follows:

Solution for Project

week_05_react_state_exercise_1's People

Contributors

i-am-ebere avatar

Watchers

 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.