redux形式:`object`类型的prop`form`无效

时间:2017-03-12 15:55:30

标签: reactjs redux redux-form

当我使用redux表单时,请收到以下错误。状态树形式中有一个[object Object]键,在图片中,category_edit键是通过调用initialize函数生成的。

Failed prop type: Invalid prop `form` of type `object` supplied to `Form(CategoryEdit)`, expected `string`.

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:0)

您收到此错误,因为reduxForm希望收到form: "string"的设置对象,其中string是您的表单名称。根据错误,您将对象传递给设置中的表单属性,例如form: { ... }。如果您可以显示您的表单代码,我可以指出一个确切的解决方法。

答案 1 :(得分:0)

我解决了这个问题,当使用表单组件时,不要输入道具。例如:

@reduxForm({
    form: 'demo'
})
class Demo extends Component {
    render () {
        return (
            <form>
                <Field component="input" label="name" name="name"></Field>
            </form>
        );
    }
}

class Container extends Component {
    render() {
        return (<Demo {...this.props}></Demo>); // !!==> remove this.props.
    }
}

原因将在稍后完成......我会找到。

答案 2 :(得分:0)

借调Honpery对自己问题的回答并稍微扩展一下。看来如果你将一个道具传递给名为'form'的表单组件,它就会变得混乱。

所以,例如:

<ContactForm
   form={this.props.form}
/>

将以悲伤告终。将其更改为:

 <ContactForm 
    formDetails={this.props.form}
 />

摆脱了那个奇怪的错误/问题。

答案 3 :(得分:0)

@DeividasKaržinauskas没错,但由于某些原因,他的回答没有被接受。将form: {...}设置为对象而不是字符串是问题发生的地方。为我删除它摆脱了错误。 更多动手示例:

错误的代码:

export default connect(
  state => ({ ...state.lotOperation, form: state.lotOperationForm }),
  dispatch => bindActionCreators(actionCreators, dispatch))(LotOperationIndex)

解决方案

export default connect(
  //notice here i'm not setting "form: {..}" to an object anymore.
  state => ({ ...state.lotOperation }),
  dispatch => bindActionCreators(actionCreators, dispatch))(LotOperationIndex)
相关问题