Skip to the content.

Read: Class 29 : Advanced State with Reducers

img

Review, Research, and Discussion

How can we ensure that an effect hook runs only once?

If we pass an empty array [] , it just renders the component only once like componentDidMount

Can useState() update more than one state variable at the same time?

You could combine the state into one state object and then you could do one setState call and there will only be one render. Unlike the setState in class components, the setState returned from useState doesn’t merge objects with existing state, it replaces the object entirely


  const [form, setState] = useState({
    username: '',
    password: ''
  });

Is useState() synchronous?

useState and setState both are asynchronous. Even though they are asynchronous, the useState and setState functions do not return promises. Therefore we cannot attach a then handler to it or use async/await to get the updated state values

Document the following Vocabulary Terms

State Hook The useState() is a Hook that allows you to have state variables in functional components , it takes the initial state as an argument and returns an array of two entries

Component Lifecycle the series of methods that are invoked in different stages of the component’s existence. The three phases are: Mounting, Updating, and Unmounting

Preparation Materials

useReducer Hook

  1. A store — an immutable object that holds the applications state data
  2. A reducer — a function that returns some state data, triggered by an action type
  3. An action — an object that tells the reducer how to change the state. It must contain a type property, and it can contain an optional payload property.

The reducer function

Preview

Which 3 things had you heard about previously and now have better clarity on?

usestate , useeffect , functional components

Which 3 things are you hoping to learn more about in the upcoming lecture/demo?

Hooks , DOM

What are you most excited about trying to implement or see how it works?

using functional components with hooks , instead of building classes

Resources