Chapter 2: Using the State Hook

  1. What problems did we run into while developing our own reimplementation of the useState hook? How did we solve these problems?
    • One problem was the initialization of the value every time the component gets rendered. We solved this problem by using a global variable to store the value.
    • Then, we had the problem that multiple Hooks write to the same global variable. To solve this problem, we stored the values in an array and kept track of the current Hook by assigning an index to each Hook.
  2. Why are conditional Hooks not possible in the React implementation of Hooks?
    • Conditional Hooks are not possible, because React uses the order of Hook definitions to keep track of the values. If we change the order of Hooks later on, the values will be assigned to different Hooks.
  3. What are Hooks and what do they deal with?
    • Hooks are functions that deal with state and effects in a React application
  4. What do we need to watch out for when using Hooks?
    • We need to ensure that the order of Hooks always stays the same, so we cannot use Hooks in loops or conditionals
  5. What are the common problems of alternative API ideas for Hooks?
    • Named Hooks have the problem of name collisions. Each Hook would have to have a unique name, even when using Hooks within libraries.
    • Hook factories require more boilerplate code, mainly instantiating each Hook twice, once outside of the component and once inside. Furthermore, they make it much harder to create custom Hooks.
  6. How do we implement conditional Hooks?
    • In simple cases, we can always define the Hook. Otherwise, we have to split up the components and conditionally render a separate component instead of conditionally rendering the Hook.
  1. How do we implement Hooks in loops?
    • In simple cases, we can store an array in the State Hook. Otherwise, we have to split up the components and render a separate component in a loop.
..................Content has been hidden....................

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