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 update state with its setter function
https://leanpub.com/code-react-sweetly
2. A state update takes effect on the next render
https://leanpub.com/code-react-sweetly
3. For multiple state updates, pass an updater function to the setter
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 state setter replaces state instead of merging it
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 in a component
https://leanpub.com/code-react-sweetly
6. Each component instance has its own isolated state
https://leanpub.com/code-react-sweetly
7. State is private to the component that declares it
https://leanpub.com/code-react-sweetly
8. A parent component can share state with its children through props
https://leanpub.com/code-react-sweetly
9. In strict mode, React may call your component, initializer, and updater functions twice (development only)
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
Previous
React Fragments
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?