React Redux表单文本字段初始值未设置

时间:2017-08-24 09:23:03

标签: reactjs redux redux-form

我正在尝试在Redux表单中为文本字段设置初始值/默认值:

const RegisterForm = ({ handleSubmit, pristine, submitting }) => (

    <form className="ui form" onSubmit={handleSubmit}>
        <div className="field">
            <Field
                component="input"
                type="text"
                name="email"
                placeholder="E-mail Address"
                className="form-control" />
        </div>
        <div className="field">
            <Field
                component="input"
                type="password"
                name="password"
                placeholder="Password"
                className="form-control" />
        </div>

        <div className="field">
            <Field
                component="input"
                type="text"
                name="inviteCode"
                defaultValue="default"
                placeholder="Invite Code (Optional)"
                className="form-control" />
        </div>
        <Button type="btn btn-default" disabled={pristine || submitting}>
            Register
        </Button>

        <p className="login-text">Already have an account? </p>
        <a href="/">Log in</a>
    </form>
);


RegisterForm.propTypes = {
    handleSubmit: PropTypes.func.isRequired,
    pristine: PropTypes.bool.isRequired,
    submitting: PropTypes.bool.isRequired
};

export default reduxForm({
    form: 'registerForm',
    initialValues: {
        inviteCode: 'Test'
    }
})(RegisterForm);

这是我设置初始值的地方:

initialValues: {
    inviteCode: 'Test'
}

这对我的inviteCode字段没有任何作用,它没有显示任何值。我在控制台中也没有出错。

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

我只需要为我的表单设置enableReinitialize : true

export default reduxForm({
    form: 'registerForm',
    initialValues: {
        inviteCode: 'Test'
    },
    enableReinitialize : true
})(RegisterForm);