如何以 redux 形式重置初始值
How to reset initial value in redux form
我正在使用 redux-form。我在状态的输入字段中显示初始值。
当我点击重置时,输入框仍然显示初始值。
如何重置输入字段?
这是我的代码:
const mapStateToProps = (state) => {
return {
initialValues: {
name:state.userInfo.data.name,
email:state.userInfo.data.email,
phone:state.userInfo.data.phone,
city:state.userInfo.data.city.name,
}
};
}
这就是我在输入字段中调用初始值的方式。
const renderField = ({ input, label, type, meta: { touched, error } }) => (
<div>
<input className="form-control control_new" {...input} placeholder={label} type={type}/>
{touched && ((error && <span>{error}</span>))}
</div>
)
<Field type="text" {...name} name="name" component={renderField} label="Enter Your Name" />
谢谢
import {reset} from 'redux-form';
...
dispatch(reset('myForm')); // requires form name
Harsha 的回答是正确的,但您也可以从修饰的表单组件中调用 this.props.reset()
,它已经知道您的表单名称。
您可以在 Simple Example 中的 "Clear Values" 按钮上查看它的运行情况。
TLDR:在您的表单上设置 enableReinitialize: true
我最近遇到了同样的问题并遇到了这个 post,none 的答案有效。终于找到了解决方案,希望这对其他人有帮助。问题是当你重置表单时那些初始值没有被重置。这是关于它的 github issue asking about it, and here is the current documentation
在单击清除按钮时调用如下不同的函数。
onClick={this.onClearChanges}
以这种方式将 initialValues 传递给函数,正如大家提到的,不要忘记设置 enableReinitialize: true。
onClearChanges = () => {
const { reset, onClearChanges, InitialValues } = this.props;
reset();
onClearChanges(InitialValues);
};
如果您没有带有调用方法的按钮,也可以在构造函数内部重置。
我正在使用抽屉导航器导航到不同的屏幕(不是带有 onClick 方法的可点击按钮)所以我不得不重新设置
constructor(props) {
super(props);
this.props.reset(); //<-- this
}
在第一个组件内部处理特定的 redux 形式,然后我设置
enableReinitialize: true
对于那个表格。
我正在使用 redux-form。我在状态的输入字段中显示初始值。 当我点击重置时,输入框仍然显示初始值。
如何重置输入字段?
这是我的代码:
const mapStateToProps = (state) => {
return {
initialValues: {
name:state.userInfo.data.name,
email:state.userInfo.data.email,
phone:state.userInfo.data.phone,
city:state.userInfo.data.city.name,
}
};
}
这就是我在输入字段中调用初始值的方式。
const renderField = ({ input, label, type, meta: { touched, error } }) => (
<div>
<input className="form-control control_new" {...input} placeholder={label} type={type}/>
{touched && ((error && <span>{error}</span>))}
</div>
)
<Field type="text" {...name} name="name" component={renderField} label="Enter Your Name" />
谢谢
import {reset} from 'redux-form';
...
dispatch(reset('myForm')); // requires form name
Harsha 的回答是正确的,但您也可以从修饰的表单组件中调用 this.props.reset()
,它已经知道您的表单名称。
您可以在 Simple Example 中的 "Clear Values" 按钮上查看它的运行情况。
TLDR:在您的表单上设置 enableReinitialize: true
我最近遇到了同样的问题并遇到了这个 post,none 的答案有效。终于找到了解决方案,希望这对其他人有帮助。问题是当你重置表单时那些初始值没有被重置。这是关于它的 github issue asking about it, and here is the current documentation
在单击清除按钮时调用如下不同的函数。
onClick={this.onClearChanges}
以这种方式将 initialValues 传递给函数,正如大家提到的,不要忘记设置 enableReinitialize: true。
onClearChanges = () => {
const { reset, onClearChanges, InitialValues } = this.props;
reset();
onClearChanges(InitialValues);
};
如果您没有带有调用方法的按钮,也可以在构造函数内部重置。
我正在使用抽屉导航器导航到不同的屏幕(不是带有 onClick 方法的可点击按钮)所以我不得不重新设置
constructor(props) {
super(props);
this.props.reset(); //<-- this
}
在第一个组件内部处理特定的 redux 形式,然后我设置
enableReinitialize: true
对于那个表格。