• [React] Fix "React Error: Rendered fewer hooks than expected"


    In this lesson we'll see an interesting situation where we're actually calling a function component and getting a dreaded React error: "Rendered fewer hooks than expected." We'll learn why that is happening, how it can be avoided, and how our "workaround" fixes the problem and avoids potential bugs.

    More info: Don't call a React function component

    Basiclly React hooks need to assoicate to one React component:

    function Counter() {
      const [count, setCount] = React.useState(0)
      const increment = () => setCount(c => c + 1)
      return <button onClick={increment}>{count}</button>
    }
    function BadCounterList() {
      const [items, setItems] = React.useState([])
      const addItem = () => setItems(i => [...i, {id: i.length}])
      return (
        <div>
          <button onClick={addItem}>Add Item</button>
          <div>{items.map(Counter)}</div>
        </div>
      )
    }

    The way we create Counter components is:

    items.map(Counter)

    Which React doesn't consider it is a React component, The hooks inside Counter function will be associated to BadCounterList. AKA: React function component doesn't work well with hooks.

    function GoodCounterList(params) {
      const [items, setItems] = React.useState([])
      const addItem = () => setItems(i => [...i, {id: i.length}])
      return (
        <div>
          <button onClick={addItem}>Add Item</button>
          <div>
            {items.map(i => (
              <Counter key={i.id} />
            ))}
          </div>
        </div>
      )
    }

    Here we create React component by using:

    <Counter key={i.id} />
  • 相关阅读:
    第25周三
    第25周二
    第25周一
    第24周日
    第24周六
    第24周五
    第24周四经验感受想法
    第24周三
    第24周二
    01背包问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12030392.html
Copyright © 2020-2023  润新知