gpt4 book ai didi

javascript - 可以在基于数组的循环中创建 useState 吗?

转载 作者:行者123 更新时间:2023-11-28 16:54:50 25 4
gpt4 key购买 nike

    const columns = ['task', 'category'];

for (const text in columns) {
const [text, setText] = useState();
}

我希望使用循环创建多个 useState 事物,但将事物连接在一起似乎是一个问题。我想要它做的是创建 consts: task, setTask |类别,设置类别

最佳答案

看看这个: https://reactjs.org/docs/hooks-rules.html

明确提及:

Only Call Hooks at the Top Level Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That’s what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls. (If you’re curious, we’ll explain this in depth below.)

关于javascript - 可以在基于数组的循环中创建 useState 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59415946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com