【问题标题】:How to update redux store with redux form values如何使用 redux 表单值更新 redux 存储
【发布时间】:2018-10-12 04:10:17
【问题描述】:

我正在使用 redux 表单和 redux 将表单值推送到存储/状态。首先,当我从不是 React 组件类的容器(INFORMATION COMPONENT)组件中将其作为道具传递时,我在表单组件中获得了未定义的状态,最后使用更新函数来更新商店。我是 React 新手,redux 表单让我失望

 // App Component


  class App extends Component {
    constructor(props) {
      super(props)
    }

    render() {
      return (
        <div className="App">
          <InformationFrom state={this.props}/>
        </div>
      );
    }
  }

  const mapStateToProps = (reduxState) => {
    return reduxState;
  }

  const mapDispatchToProps = (dispatch) => {
    return bindActionCreators(actionCreators, dispatch);
  }

  export default connect(
    mapStateToProps, mapDispatchToProps
  )(App)





// InformationForm Component

    export default class InformationFrom extends React.Component {
        render() {
            return (
                <div>
                    {console.log("infoForm props >> ", this.props)}
                    <SimpleForm/>
                </div>
            )
        }
    }


// Form stateless function component


    const SimpleForm = props => {
        const { handleSubmit, pristine, reset, submitting } = props
        const state = this.props.state; // is undefined

        return (

            <div>
                { console.log("from state here >> ", this.props) }
                <form onSubmit={handleSubmit(this.updateStore(values,state))} name="InformForm">
                    <div>
                        <div>
                            <Field name="firstName" component={renderInput} type="text" placeholder="First Name" label={"First Name"} />
                            <Field name="lastName" component={renderInput} type="text" placeholder="Last Name" label={"Last Name"} />
                            <Field name="email" component={renderInput} type="text" placeholder="Email" label={"Email"} />
                        </div>
                    </div>

                    <div style={style.normal}>
                        <button type="submit" disabled={submitting}>
                            Submit
            </button>
                        <button type="button" disabled={submitting} onClick={reset}>
                            Clear Values
            </button>
                    </div>
                </form>

            </div>
        )
    }

    function updateStore(values, state){
    let newStore = {};

    newStore = {

    }


        return newStore;
    }

    const validate = values => {
        const errors = {}
        if (!values.firstName) {
            errors.firstName = "Required!"
        }
        return errors;
    }

    const style = {
        normal: {
            margin: '10px'
        }
    }

    export default reduxForm({
        form: 'simple', // a unique identifier for this form
        destroyOnUnmount: false,
        validate
    })(SimpleForm)

【问题讨论】:

    标签: reactjs react-redux jsx redux-form


    【解决方案1】:

    你没有在 InformationFrom 中将 props 传递给 SimpleForm,所以需要更新

    export default class InformationFrom extends React.Component {
            render() {
                return (
                    <div>
                        {console.log("infoForm props >> ", this.props)}
                        <SimpleForm/>
                    </div>
                )
            }
        }
    

    <SimpleForm  {...this.props} />
    

    并通过连接导出以从 redux 获取状态

    export default connect((state)=>state)( reduxForm({
            form: 'simple', // a unique identifier for this form
            destroyOnUnmount: false,
            validate
        })(SimpleForm) );
    

    【讨论】:

    • 我这样做了,是不是让 SimpleForm 中的函数可以使用道具??
    • 你还需要 simpleForm 与 redux 连接
    猜你喜欢
    • 2021-10-10
    • 1970-01-01
    • 2018-10-02
    • 2020-02-12
    • 2020-10-15
    • 2017-07-10
    • 2022-01-06
    • 2023-03-13
    • 2017-05-29
    相关资源
    最近更新 更多