[英]React multi-step form using react-select not updating state
I'm creating a multi-step form using React and one of the steps uses react-select.我正在使用 React 创建一个多步骤表单,其中一个步骤使用 react-select。
The parent component has the following change handler:父组件具有以下更改处理程序:
const handleChange = input => e => {
setFormData({ ...formData, [input]: e.target.value});
}
It works fine for the majority of my fields and steps but it seems this isn't usable for react-select due to react-select returning an object.它适用于我的大多数字段和步骤,但由于 react-select 返回 object,这似乎不适用于 react-select。 So I wrote a separate handler in the parent component as follows:
所以我在父组件中写了一个单独的处理程序如下:
const handleSelectChange = e => {
setFormData({ ...formData, [input]: e });
}
And in the Select component I have the following:在 Select 组件中,我有以下内容:
<Select
defaultValue={values.purpose}
name="purpose"
options={purposeOptions}
className="basic-single"
classNamePrefix="select"
onChange={handleSelectChange('purpose')}
/>
However I get the error that 'handleSelectChange' is not a function.但是,我收到“handleSelectChange”不是 function 的错误。 I have tried changing the handler code to this:
我尝试将处理程序代码更改为:
const handleSelectChange = e => {
setFormData({ ...formData, purpose: e });
}
and removing the parentheses in the Select component so onChange now reads:并删除 Select 组件中的括号,因此 onChange 现在显示为:
onChange={handleSelectChange}
This has removed the error but now the stateField is not updating with the value from the select.这已经消除了错误,但现在 stateField 没有使用 select 中的值进行更新。
Any advice will be much appreciated.任何建议将不胜感激。
Change onChange={handleSelectChange('purpose')}
to将
onChange={handleSelectChange('purpose')}
更改为
onChange={() => handleSelectChange('purpose')}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.