我是否必须将redux-form挂载到我的状态根目录中的“form”键?

时间:2016-09-02 08:58:44

标签: reactjs redux redux-form

getting started page说:

import { createStore, combineReducers } from 'redux'
import { reducer as formReducer } from 'redux-form'

const reducers = {
  // ... your other reducers here ...
  form: formReducer     // <---- Mounted at 'form'
}
const reducer = combineReducers(reducers)
const store = createStore(reducer)

但是reduxForm() documentation说:

  

form:String [required]

     

表单的名称以及表单状态将在redux-form reducer下挂载的键

我正在运行Simple Form example。我有这段代码:

var reduxFormReducer = reduxForm({
  formKey: 'personal'  // a unique identifier for this form
})(SimpleForm);

var reducer = combineReducers({
  formKey: reduxFormReducer
});

但它显示了这个错误:

  

警告:道具类型失败:form中未指定必需道具Form(SimpleForm)。在Form(SimpleForm)中(由Connect(Form(SimpleForm))创建)

我试过这个:

var reduxFormReducer = reduxForm({
  form: 'formKey'  // a unique identifier for this form
})(SimpleForm);

var reducer = combineReducers({
  formKey: reduxFormReducer
});

它没有显示错误,但编辑字段为空且无法更改。

1 个答案:

答案 0 :(得分:3)

我希望您在减速机中错过了,请使用密钥名称form

var reduxFormReducer = reduxForm({
  form: 'formKey'  // a unique identifier for this form
})(SimpleForm);

var reducer = combineReducers({
  form: reduxFormReducer // <- change formKey to form here 
});

参考:http://redux-form.com/6.0.2/docs/GettingStarted.md/

<强>更新

表格减速机。应该在form安装到Redux状态。

如果您绝对必须将其安装在form以外的其他位置,则可以向getFormState(state)装饰器提供reduxForm()函数,以获取已安装的Redux状态的片段redux-form减速机。