gpt4 book ai didi

reactjs - react 动态添加选择输入字段

转载 作者:行者123 更新时间:2023-12-04 14:54:36 25 4
gpt4 key购买 nike

我正在尝试动态创建一个选择字段。单击添加按钮时,它将动态添加选择字段。我遇到了值不变的问题。

我的代码可以在codesandbox上查看:
https://codesandbox.io/s/react-select-field-dynamically-uo4dy?file=/src/App.js

最佳答案

看看我的变化:

Forked sandbox

// I added a 3rd argument for the name:    
const handleRoomChange = (option, index, name) => {
const value = option.value; // you had this as 'const { value } = option.value' which dereferences value twice
console.log(value);
const list = [...roomInputs];
list[index][name] = value; //you had this as list[index][value]
setRoomInputs(list);
};


// onChange passes the name as 3rd argument to handleRoomChange
<Select
name="boardBasic"
placeHolder="Board"
value={options.value}
onChange={option => handleRoomChange(option, i, "boardBasic")}
options={options}
styles={{
menu: (provided) => ({ ...provided, zIndex: 9999 })
}}
/>

关于reactjs - react 动态添加选择输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68316256/

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