英文:
How can I use variable in useState hook in react?
问题
I'd like to declare the same number of useState hooks with the userInputs array's length.
And I want to name them like userInput0, userInput1, userInput2,,,userInputN
But "const [userInput+index.toString(), setUserInput] = useState('')" throws an error.
How can I fix it?
'use client';
import { useState } from 'react';
userInputs = ['hoge', '123', 'aaa'];
userInputs.array.forEach((element, index) => {
const [userInput+index.toString(), setUserInput] = useState('')
});
英文:
I'd like to declare the same number of useState hooks with the userInputs array's length.
And I want to name them like userInput0, userInput1, userInput2,,,userInputN
But "const [userInput+index.toString(), setUserInput] = useState('')" throws an error.
How can I fix it?
'use client';
import { useState } from 'react';
userInputs = ['hoge', '123', 'aaa'];
userInputs.array.forEach((element, index) => {
const [userInput+index.toString(), setUserInput] = useState('')
});
答案1
得分: 2
我认为尽管这可能是你可以做的事情,但我不建议这样做,因为它可能导致意外的行为。
在使用 hooks 时有一些规则,你正在违反其中之一。
不要在循环、条件或嵌套函数内调用 Hooks
https://legacy.reactjs.org/docs/hooks-rules.html
我建议你创建一个名为<Input>的组件,带有它自己的状态,并使用你的数组渲染3个不同的组件,每个组件都带有它们自己的状态。
英文:
I think despite that this is something that you could probably do I wouldn't recommend it because it could lead to unexpected behavior
There are some rules in order to use hooks and you are breaking one of them
Don’t call Hooks inside loops, conditions, or nested functions
https://legacy.reactjs.org/docs/hooks-rules.html
I recommend you that you create <Input> component with its own state and with your array render 3 different components with their states
答案2
得分: 1
要动态声明多个类似于"userInput0"、"userInput1"、"userInput2"等的useState钩子,您不能直接将变量名连接为字符串。但是,您可以使用对象或数组来存储状态值来实现这一目标。以下是一个示例,演示如何完成这个任务:
const userInputs = ['hoge', '123', 'aaa'];
const [inputs, setInputs] = useState({});
userInputs.forEach((element, index) => {
const stateName = 'userInput' + index;
if (!inputs.hasOwnProperty(stateName)) {
setInputs((prevInputs) => ({ ...prevInputs, [stateName]: '' }));
}
});
请注意,这是一个JavaScript代码示例,用于动态创建多个useState钩子,每个都有类似"userInput0"的名称。
英文:
To dynamically declare multiple useState hooks with names like "userInput0", "userInput1", "userInput2", etc., you can't directly concatenate the variable name as a string. However, you can achieve this using an object or an array to store the state values. Here's an example of how you can accomplish it:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const userInputs = ['hoge', '123', 'aaa'];
const [inputs, setInputs] = useState({});
userInputs.forEach((element, index) => {
const stateName = 'userInput' + index;
if (!inputs.hasOwnProperty(stateName)) {
setInputs((prevInputs) => ({ ...prevInputs, [stateName]: '' }));
}
});
<!-- end snippet -->
答案3
得分: 0
如果您想要动态创建变量,例如var0
、var1
、var2
,那么只需使用一个数组。
const userInputs = [];
const setUserInputs = [];
userInputs.forEach((element, index) => {
const [xuserInput, setUserInput] = useState('');
userInputs.push(xuserInput);
setUserInputs.push(setUserInput);
});
英文:
If you are wanting to create variables dynamically like var0
, var1
, var2
, then just use an array.
const userInputs = [];
const setUserInputs = [];
userInputs.forEach((element, index) => {
const [xuserInput, setUserInput] = useState('');
userInputs.push(xuserInput);
setUserInputs.push(setUserInput);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论