使用道具的状态初始化redux表单控件

时间:2019-08-29 05:57:38

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

使用的包裹

Abc
123

这是组件

^([A-Za-z]+)\s*(\[?)\s*([A-Za-z]*)(\d*)\s*(\]?)\s*$

面临的问题:

"react-redux": "^5.0.6",
"redux-form": "^8.2.5",
"react": "^16.4.2",

2 个答案:

答案 0 :(得分:1)

您的组件中有两个错误

1)您正在使用类组件,这意味着它需要使用 render方法(您已经错过了)扩展 Component (已完成)。它,它将返回JSX标记。

class EditProfileForm extends Component {
  render() {
    return (
      <div>
        <Field name="firstName" component="input"/>
      </div>
    );
  }
}

2) initialValues 属性必须是一个对象而不是值

const mapStateToProps = state => ({
  initialValues: state.profile
});

如果您尝试创建功能组件,这是解决方案

let EditProfileForm = () => {
  return (
    <div>
      <Field name="firstName" component="input"/>
    </div>
  );
};

EditProfileForm = reduxForm({
  form: 'EditProfileForm'
})(EditProfileForm);

const mapStateToProps = state => ({
  initialValues: state.profile
});

export default connect(mapStateToProps)(EditProfileForm);

答案 1 :(得分:0)

您的组件不正确,请提供以下示例

class EditProfileForm extends React.Component {
  render() {
    return (
      <div>
       <Field name="firstName" component="input"/>
      </div>
    )
  }
}