我想在ReactSelect的defaultValue中显示默认值,但是当状态更新时,defaultValue并没有更新。默认显示未更新。
我该怎么做才能避免这种现象? 如果您能告诉我更多相关信息,我将不胜感激。
constructor(props) {
super(props)
this.state = {
item: props.item,
};
}
const set = { label: this.state.item.name, value: this.state.item.id }
<Select
options={groupOptions}
defaultValue={set}
/>
最佳答案
将value
作为prop
传递。
这是代码:
<Select
name="something"
options={groupOptions}
defaultValue={set}
value={set}
onChange={(e) => inputChangeHandler(e.target.value)}
/>
注意:inputChangeHandler
函数将更新state
。
祝你编码愉快:)
关于reactjs - 当状态更新时,我们也想更新 React-Select 的 defaultValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66970085/