如何使用自定义JSON中的mapStateToProps作为prop来初始化redux表单中的值?

时间:2017-09-21 19:06:53

标签: json reactjs redux react-redux redux-form

我正在构建一个redux表单,我希望通过不同JSON对象中存在的props传递initialValues。

我作为道具传递的JSON对象的结构是: -

"Team": {
      "Manager": {
           "name": "Tom"
      },
      "members": {
        "PD" : {
          "SE": {
            "name" : "SAM"
          }
        }
      }
 },

我的redux形成了2个字段,我需要填充初始状态为Tom和SAM

我正在调用具有redux-form的类,如下所示: -

ReactDOM.render(
    <Provider store={store}><Emp empJSON={}/></Provider>,
    document.querySelector('#tenant-content')
);

Emp类是这样的: -

Emp.propTypes = {
    empJSON: PropTypes.object,

};

Emp.defaultProps ={
    empJSON:{},
}



export default reduxForm({
    form: 'empForm', // a unique identifier for this form
    onSubmit: submit,

},mapStateToProps )(Emp);

renderPartnerName.propTypes  = {
    input: PropTypes.object,
};

我也有一个功能

function mapStateToProps(state, ownProps) {
    // state will be the state of your redux store
    // ownProps will be your component's props
    console.log(state)
    console.log(ownProps)

    return {
        initialValues: {
            EmployeeName: ownProps.members.PD.SE.name,
        }

}}

然而,控制台上没有打印任何内容,也没有设置初始值。

我做错了吗?

1 个答案:

答案 0 :(得分:0)

我不确定有没有办法直接将npm install react-router@3 --save 传递给redux-form - 你错过了mapStateToProps吗?

connect

请参阅http://redux-form.com/7.0.4/docs/faq/howtoconnect.md/