Code React Sweetly
/
React State
React State
https://leanpub.com/code-react-sweetly
How to Access and Modify a Function Component’s State
https://leanpub.com/code-react-sweetly
What Exactly Is the useState() Function?
https://leanpub.com/code-react-sweetly
Syntax of the useState() Hook
https://leanpub.com/code-react-sweetly
Example 1: Call the useState Hook without an initialState argument
https://leanpub.com/code-react-sweetly
Example 2: Call the useState Hook with an initialState argument
https://leanpub.com/code-react-sweetly
Why Does useState() Return an Array?
https://leanpub.com/code-react-sweetly
Example 1: Using bracket notation to access a component’s state
https://leanpub.com/code-react-sweetly
Example 2: Using array destructuring to access a component’s state
https://leanpub.com/code-react-sweetly
How to Update a Component’s State
https://leanpub.com/code-react-sweetly
How to Reset a Component’s States
https://leanpub.com/code-react-sweetly
Example 1: The manual way to reset a component’s states
https://leanpub.com/code-react-sweetly
Example 2: The
key
prop technique of resetting a component’s states
https://leanpub.com/code-react-sweetly
Important Things to Know About React State
https://leanpub.com/code-react-sweetly
1. Always modify a state with the setter function
https://leanpub.com/code-react-sweetly
2. A set function call affects only the next rendering process
https://leanpub.com/code-react-sweetly
3. For successive state updates, pass a function into your state’s setter method
https://leanpub.com/code-react-sweetly
Example 1: Using the state variable successively in the ongoing rendering to update a component’s state
https://leanpub.com/code-react-sweetly
Example 2: Using updater functions successively in the ongoing rendering to update a component’s state
https://leanpub.com/code-react-sweetly
4. A function component’s setter function does not auto-merge a new state into the existing one
https://leanpub.com/code-react-sweetly
Example 1: Updating a component’s state without the spread operator
https://leanpub.com/code-react-sweetly
Example 2: Using a spread operator to merge a function component’s states
https://leanpub.com/code-react-sweetly
Example 3: Using spread operator to merge a function component’s state array data types
https://leanpub.com/code-react-sweetly
5. You can call useState() multiple times
https://leanpub.com/code-react-sweetly
6. Each component invocation has a completely isolated state
https://leanpub.com/code-react-sweetly
7. State is a private feature of the component declaring it
https://leanpub.com/code-react-sweetly
8. A parent component can share its states with its children
https://leanpub.com/code-react-sweetly
9. A component, initializer, and updater function run twice in strict and development mode
https://leanpub.com/code-react-sweetly
Example 1: Using an impure updater function to update a state
https://leanpub.com/code-react-sweetly
Example 2: Using a pure updater function to update a state
https://leanpub.com/code-react-sweetly
Time to Practice with React State
https://leanpub.com/code-react-sweetly
How Did You Go About Creating Your Login (out) Button?
https://leanpub.com/code-react-sweetly
1. Create a new project directory
https://leanpub.com/code-react-sweetly
2. Install React and ReactDOM
https://leanpub.com/code-react-sweetly
3. Create a components directory
https://leanpub.com/code-react-sweetly
4. Create the root component
https://leanpub.com/code-react-sweetly
5. Create the entry script
https://leanpub.com/code-react-sweetly
6. Create the stylesheet
https://leanpub.com/code-react-sweetly
7. Create the web page
https://leanpub.com/code-react-sweetly
8. Run your application
https://leanpub.com/code-react-sweetly
How Does the React State Hook Work Behind the Scenes?
https://leanpub.com/code-react-sweetly
Initial rendering
https://leanpub.com/code-react-sweetly
Hook calls
https://leanpub.com/code-react-sweetly
Subsequent renderings
https://leanpub.com/code-react-sweetly
Up next
React Trigger vs. Render vs. Commit vs. Paint
In this chapter
React State
How to Access and Modify a Function Component’s State
What Exactly Is the useState() Function?
Why Does useState() Return an Array?
How to Update a Component’s State
How to Reset a Component’s States
Important Things to Know About React State
Time to Practice with React State
How Did You Go About Creating Your Login (out) Button?
How Does the React State Hook Work Behind the Scenes?