【问题标题】:How to store a complex form object into redux state on submit?如何在提交时将复杂的表单对象存储到 redux 状态?
【发布时间】:2020-07-06 06:58:06
【问题描述】:

React/Redux 的新手,我正在努力让用户可以拥有尽可能多的 choices 和不同的 choicecustomAttribute

现在我有一个按钮,他们可以创建 UI 来动态创建一个新的文本字段来输入另一个 choicecustomAttribute,但我完全难倒如何在 redux 中存储这样的东西。

我看到了其他关于如何存储用户名和/或密码的问题和答案,但没有看到任何关于如何存储具有我的完整状态的对象的示例。

我的组件

class CreateRIG extends Component<any, any> {
  constructor(props) {
    super(props);

    this.state = {
      rigName: '',
      desc: '',
      choices: [{
        choice: '',
        customAttribute: ''
      }]
    }
  }

  createUI() {
    return this.state.choices.map((el, i) => (
      <div key={i}>
        <FormControl>
          <Select
            id="choice"
            name="choice"
            onChange={this.handleChange.bind(this, i)}
            value={el.choice || ''}
          >
            <MenuItem value=''>
              <em>None</em>
            </MenuItem>
            <MenuItem value='ID'>ID</MenuItem>
            <MenuItem value='PSA'>PSA</MenuItem>
            <MenuItem value='ExternalID'>ExternalID</MenuItem>
          </Select>
        </FormControl>
        <div>
          <TextField name="customAttribute" label="Attribute"
            onChange={this.handleChange.bind(this, i)} value={el.customAttribute || ''} />
    ))
  }

  handleSubmit = (event) => {
    event.preventDefault();
    console.log(this.state);
  }

  render() {
    return (
          <form onSubmit={this.handleSubmit}>
                    <div>
                      <TextField name="rigName"
                        value={this.state.rigName}
                        onChange={event => this.setState({ rigName: event.target.value })}/>
                    </div>
                    <div className={styles.spacing}>
                      <TextField name="description" 
                        onChange={event => this.setState({ desc: event.target.value })}/>
                    </div>
                  {this.createUI()}
                  <Button type="submit" 
                  onClick={this.props.onSubmitForm}>NEXT</Button>
          </form>
    );
  }
}

const mapDispatchToProps = dispatch => {
  return {
    onSubmitForm: (rigName, desc, choice, customAttribute) => dispatch({ type: 'FORM_DATA'})
  }
}

const connectedCreateRIGPage = connect(mapStateToProps, mapDispatchToProps)(CreateRIG);
export { connectedCreateRIGPage as CreateRIG };

export default CreateRIG;

Action.tsx

export const createRIGActions = {
    searchInput
};

function searchInput(rigName, desc, choice, customAttribute) {
    return {
        type: createRIGConstants.STORE_FORM,
        rigName,
        desc,
        choice,
        customAttribute
    }
}

Reducer.tsx

const initialState = { 
    results: [] 
};

export function createRIGReducer(state = initialState, action) {
    switch (action.type) {
        case createRIGConstants.STORE_FORM:
            return {
                ...state,
                results: state.results
            }
            // Need to somehow get the data from the form

        default:
            return state
    }
}

如何将表单中的复杂对象存储到提交时的 redux 状态?现在我的 onSubmit 是控制台记录我想要的正确对象,这很好

【问题讨论】:

    标签: javascript arrays reactjs object redux


    【解决方案1】:

    我相信传递给dispatch 的内容很重要。您也可以在此处添加payload

    尝试以下方法:

    const mapDispatchToProps = dispatch => {
      return {
        onSubmitForm: (rigName, desc, choice, customAttribute) => dispatch({ type: 'FORM_DATA', payload: {rigName, desc, choice, customAttribute}})
      }
    }
    

    然后在您的减速器中,您可以访问 payload,如下所示:

    export default (state=initialState, action) => {
        switch(action.type) {
            const { payload } = action;
            case 'FORM_DATA':
                return {
                    ...state,
                    // here you can use data from payload to update the state
                };
    

    进一步阅读 Redux 文档:Managing Normalized Data

    我希望这会有所帮助!

    【讨论】:

    • 我怎样才能改变它以在调度中发送对象数组,可能不止一个?因为在我的状态choices 包含choicecustomAttribute
    • 我认为如果您将onClick={this.props.onSubmitForm} 部分修改为onClick={() =&gt; this.props.onSubmitForm( &lt;list-of-data&gt; )},那么您可以将您真正需要的传递给onSubmitForm
    • 谢谢!不幸的是,它大喊大叫 onSubmitForm 不是一个函数,即使没有传递数据列表
    • @awfuldev2020 你需要.bind(),就像你在代码中使用handleChange一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2011-03-26
    • 2018-05-03
    • 2017-08-15
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多