Chapter 12: Redux and Hooks

  1. What kind of state should Redux be used for?
    • Redux should be used for global state, which is state that is used in multiple components across our app
  2. Which elements does Redux consist of?
    • Redux consists of the store (an object that describes the full state of our application), actions (objects that describe state modifications), action creators (functions that create action objects), reducers (functions that take the current state and an action object and return a new state), and connectors (higher-order components that connect an existing component to Redux)
  3. What are the three principles of Redux?
    • Single source of truth (data should always have a single source)
    • Read-only state (it is not possible to modify state directly, only through dispatching actions)
    • State changes are processed with pure functions (given the same state and action, reducers will always return the same new state)
  1. Why do we define action types?
    • Action types avoid making typos when defining or comparing the type property of actions
  2. How can we connect components to Redux?
    • We can either use the connect higher-order component, or Dispatch and Selector Hooks
  3. Which Hooks can we use with Redux?
    • useDispatch to get the dispatch function, useSelector to get a certain part of the state, and useStore to get the Redux store (for special use cases, such as replacing reducers)
  4. Why should we create reusable selectors?
    • Reusable selectors can be used in multiple components. Furthermore, they memoize the result and only recompute it when the state changes.
  5. How can we migrate a Redux application?
    • We should first replace simple local state, such as input field values, with State Hooks. Then replace complex local state with Reducer Hooks. We keep global state, which is used across multiple components, in the Redux store. Finally, we use the Selector and Dispatch Hooks instead of the connect higher-order component.
  1. What are the trade-offs of Redux?
    • The pros of using Redux are: It provides a certain project structure that allows us to easily extend and modify code later on, there are fewer possibilities for errors in our code, it has better performance than simply using React context for state, and it makes our App component much simpler by offloading state management and action creators to Redux
    • The downsides of using Redux are: It requires a lot of boilerplate code, the project structure becomes more complicated, and it requires a wrapper component (Provider) to connect the app to the store
  2. When should we use Redux?
    • We should use Redux only for applications that require complex state changes. For simple projects, Reducer Hooks or even just State Hooks might be enough.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.150.163