zoukankan      html  css  js  c++  java
  • [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} />
  • 相关阅读:
    zhuanzai
    安装 Python
    解决删除/升级Python导致Ubuuntu无法进入桌面的问题
    硬盘安装ubuntu120.04分区方案
    ping: sendto: Network is unreachable
    电脑系统右键反应很慢问题
    安装Ubuntu下的开发工具
    设置文件夹为超级权限
    Ubuntu12.04配置静态ip地址
    马拉车算法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12030392.html
Copyright © 2011-2022 走看看