Reactjs Redux表单-初始值不随状态更新
我在使用redux表单设置初始表单字段值时遇到一些问题 我正在使用redux FormV6.0.0-rc.3和react v15.3.0 这是我的问题,我有一个用户网格,当点击一个用户行时,我导航到一个编辑用户页面,并在url中包含用户id。然后在编辑用户页面上,我获取id并调用fetchUser(this.props.params.id),它是返回this.state.users的操作创建者。然后,我尝试通过调用以下命令来设置表单初始值:Reactjs Redux表单-初始值不随状态更新,reactjs,redux,react-redux,redux-form,Reactjs,Redux,React Redux,Redux Form,我在使用redux表单设置初始表单字段值时遇到一些问题 我正在使用redux FormV6.0.0-rc.3和react v15.3.0 这是我的问题,我有一个用户网格,当点击一个用户行时,我导航到一个编辑用户页面,并在url中包含用户id。然后在编辑用户页面上,我获取id并调用fetchUser(this.props.params.id),它是返回this.state.users的操作创建者。然后,我尝试通过调用以下命令来设置表单初始值: function mapStateToProps(st
function mapStateToProps(state) {
return { initialValues: state.users.user }
}
根据我的理解,这应该将初始值设置为state.users.user,并且每当更新此状态时,初始值也应该更新。我不是这样的。正在将InitialValues设置为先前单击的用户行(即this.state.users.user的先前状态)。因此,我决定对此进行测试,并向该组件添加一个按钮,单击该按钮后,我将再次调用fetchUser,其中包含一个硬编码的用户id:
this.props.fetchUser('75e585aa-480b-496a-b852-82a541aa0ca3')代码>
这将正确更新状态,但InitialValue的值不会更改。当状态更新时,它不会得到更新。我在一个旧版本的redux表单上测试了这个完全相同的过程,它按预期工作
我在这里做错了什么,或者这是我使用的版本的问题
用户编辑表格-
class UsersShowForm extends Component {
componentWillMount() {
this.props.fetchUser(this.props.params.id);
}
onSubmit(props){
console.log('submitting');
}
changeUser() {
this.props.fetchUser('75e585aa-480b-496a-b852-82a541aa0ca3');
}
renderTextField(field) {
return (
<TextField
floatingLabelText={field.input.label}
errorText={field.touched && field.error}
fullWidth={true}
{...field.input}
/>)
}
render() {
const { handleSubmit, submitting, pristine } = this.props;
return(
<div>
<form onSubmit={handleSubmit(this.onSubmit.bind(this))} className="mdl-cell mdl-cell--12-col">
<Field name="emailAddress" component={this.renderTextField} label="Email Address"/>
<Field name="firstName" component={this.renderTextField} label="First Name"/>
<Field name="lastName" component={this.renderTextField} label="Last Name"/>
</form>
<RaisedButton onClick={this.changeUser.bind(this)} label="Primary" primary={true} />
</div>
);
}
}
function mapStateToProps(state) {
return { initialValues: state.users.user }
}
UsersShowForm = reduxForm({
form: 'UsersShowForm'
})(UsersShowForm)
UsersShowForm = connect(
mapStateToProps,
actions
)(UsersShowForm)
export default UsersShowForm
减速器指数-
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import usersReducer from './users_reducer';
const rootReducer = combineReducers({
form: formReducer,
users: usersReducer
});
export default rootReducer;
在更新到redux form v6.0.0-rc.4之后,我也面临同样的问题
我解决了将enableReinitialize设置为true的问题
UsersShowForm = reduxForm({
form: 'UsersShowForm',
enableReinitialize: true
})(UsersShowForm)
要使用资源中的数据预填充redux表单
表单,可以使用initialValues
属性,当使用reduxForm
连接器装饰组件/容器时,该属性会自动读取。初始值
中的键必须与表单字段中的名称
匹配,这一点很重要
注意:首先需要应用reduxForm()
decorator,然后从redux应用connect()
。它不会反过来工作。
使用redux表格7.2.3:
const connectedReduxForm = reduxForm({
form: 'someUniqueFormId',
// resets the values every time the state changes
// use only if you need to re-populate when the state changes
//enableReinitialize : true
})(UserForm);
export default connect(
(state) => {
// map state to props
// important: initialValues prop will be read by redux-form
// the keys must match the `name` property of the each form field
initialValues: state.user
},
{ fetchUser } // map dispatch to props
)(connectedReduxForm)
从官方文件:
提供给initialValues prop或reduxForm()配置参数的值将加载到表单状态,并在此后被视为“原始”。它们也是调度reset()时返回的值。除了保存“原始”值外,初始化表单将覆盖任何现有值
在中找到更多信息和完整示例。在我的例子中,我有Redux表单,2个路由,url参数有一些小的更改。
在组件之间切换时,未更新道具。
它一直显示空白表格
示例:baseurl/:id/personal->baseurl/:id/employment
调试后,我发现MapStateTops在设置了初始值的情况下不会触发
<React.Fragment>
<Route
exact={true}
path="/path1"
component={PersonalDetails}
key="personal"
/>
<Route
exact={true}
path="/path1/employment"
component={Employment}
key="employment"
/>
</React.Fragment>
使用上面的示例:
UsersShowForm = reduxForm({
form: 'UsersShowForm',
enableReinitialize: true,
destroyOnUnmount: false
})(UsersShowForm)
阅读更多信息:您的用户还原器没有名称,因此当您在索引还原器中导入时,它不会导入用户还原器,因为它不存在。当你在NPM中构建它的时候,你会有错误吗?我不知道你的意思。我正在导出我的Users reducer,然后在索引中导入它-从“/Users\u reducer”导入Users reducer。我没有收到任何错误,我的用户reducer在所有其他情况下都能正常工作。我的意思是,当您使用“导出默认函数()”时,您不会给函数命名。我希望看到“导出默认函数usersReducer()”,以便索引缩减器使用itA名称,这与此无关。我从一个文件导出一个函数,然后从另一个文件导入该函数。当我从“/users\u reducer”调用import users reducer时代码>,usersReducer现在等于从我的users reducer文件导出的函数。这解决了问题!我花了几个小时试着调试它。谢谢。也解决了这个问题…它也写在文档中:也花了几个小时试图解决这个问题。文档中应该更加明确。谢谢你!我很高兴我找到了这个帖子,非常感谢。花了几个小时试图解决这个问题。
enableReinitialize: true,
destroyOnUnmount: false
UsersShowForm = reduxForm({
form: 'UsersShowForm',
enableReinitialize: true,
destroyOnUnmount: false
})(UsersShowForm)