如何从提交表单中获取输入值并存储在redux存储变量中?

时间:2018-05-27 07:31:05

标签: javascript reactjs redux

我已经提交了银行帐户提交表格。我想将表单中输入的数据保存到redux存储中。如何从表单中获取输入值并将其存储在redux存储变量中?

我的client.js文件有redux store,form.js是我需要获取输入值的组件。 client.js:

import { combineReducers, createStore } from 'redux';

const addUserReducer = (state={}, action) => {
    switch(action.type){
        case: "CHANGE_FIRSTNAME"{
            state = {...state, firstname:action.payload }
            break;
        }
        case: "CHANGE_LASTNAME"{
            state = {...state, lastname:action.payload }
            break;
        }
        case: "CHANGE_EMAILID"{
            state = {...state, emailid:action.payload }
            break;
        }
        case: "CHANGE_IBAN"{
            state = {...state, iban:action.payload }
            break;
        }
        case: "CHANGE_BANKNAME"{
            state = {...state, bankname:action.payload }
            break;
        }
    } 
    return state;
}

const reducers = combineReducers({
    addUser:addUserReducer
})


const store = createStore(reducers);

store.subscribe(() => {
    console.log('Store changed', store.getState());
})

store.dispatch({type: "CHANGE_FIRSTNAME", payload:"Will"});
store.dispatch({type: "CHANGE_LASTNAME", payload:"Groot"});
store.dispatch({type: "CHANGE_EMAILID", payload:"xyz@gmail.com"});
store.dispatch({type: "CHANGE_IBAN", payload:3234243242});
store.dispatch({type: "CHANGE_BANKNAME", payload:"XYZ"});

form.js:

import React, { Component } from "react";
import "./form.css";

class Form extends Component {
  render() {
    return (
      <div>
        <div id="center">
          <form>
            <div className="form-group">
              <label for="firstname">First Name:</label>
              <input type="firstname" className="form-control" id="firstname" />
            </div>

            <div className="form-group">
              <label for="lastname">Last Name:</label>
              <input type="lastname" className="form-control" id="lastname" />
            </div>

            <div className="form-group">
              <label for="email">Email address:</label>
              <input type="email" className="form-control" id="email" />
            </div>

            <div className="form-group">
              <label for="bankacc">IBAN:</label>
              <div id="deletebank" className="items">
                <input type="bankacc" className="form-control" id="bankacc" />
                <button type="button" class="btn btn-default btn-sm">
                  <span class="glyphicon glyphicon-trash" />
                </button>
              </div>
            </div>

            <div className="form-group">
              <label for="bankname">Bank Name:</label>
              <input type="bankname" className="form-control" id="bankname" />
            </div>

            <div className="form-group">
              <button type="button" className="btn addbank">
                + Add bank account
              </button>
            </div>

            <div className="form-group">
              <button type="button" class="btn btn-success">
                Submit
              </button>
            </div>
          </form>
        </div>
      </div>
    );
  }
}

export default Form;

我的表单截图:

enter image description here

1 个答案:

答案 0 :(得分:2)

我建议使用react-redux将您的组件与redux存储连接起来,但如果没有它,它仍然是可行的:

创建将更新商店中特定变量的动作创建者:

import { bindActionCreators } from "redux";

const updateFirstName = name => ({ type: "CHANGE_FIRSTNAME", payload: name });
const updateLastName = lastName => ({
  type: "CHANGE_LASTNAME",
  payload: lastName
});
const updateEmail = email => ({ type: "CHANGE_EMAILID", payload: email });
const updateIban = iban => ({ type: "CHANGE_IBAN", payload: iban });
const updateBankName = bankName => ({
  type: "CHANGE_BANKNAME",
  payload: bankName
});

现在使用dispatch绑定您的操作创建者,因此调用actionCreators.updateFirsName('something')实际上会将操作分派给商店。

export const actionCreators = bindActionCreators(
  {
    updateFirstName,
    updateLastName,
    updateEmail,
    updateIban,
    updateBankName
  },
  store.dispatch
);

现在,只要输入发生变化,您就只需要调用每个商店更新功能:

import React, { Component } from "react";
import "./form.css";
import { actionCreators } from "/path/to/store";

class Form extends Component {
  render() {
    return (
      <div>
        <div id="center">
          <form>
            <div className="form-group">
              <label for="firstname">First Name:</label>
              <input
                type="firstname"
                className="form-control"
                id="firstname"
                onChange={e => actionCreators.updateFirstName(e.target.value)}
              />
            </div>

            <div className="form-group">
              <label for="lastname">Last Name:</label>
              <input
                type="lastname"
                className="form-control"
                id="lastname"
                onChange={e => actionCreators.updateLastName(e.target.value)}
              />
            </div>

            <div className="form-group">
              <label for="email">Email address:</label>
              <input
                type="email"
                className="form-control"
                id="email"
                onChange={e => actionCreators.updateEmail(e.target.value)}
              />
            </div>

            <div className="form-group">
              <label for="bankacc">IBAN:</label>
              <div id="deletebank" className="items">
                <input
                  type="bankacc"
                  className="form-control"
                  id="bankacc"
                  onChange={e => actionCreators.updateIban(e.target.value)}
                />
                <button type="button" class="btn btn-default btn-sm">
                  <span class="glyphicon glyphicon-trash" />
                </button>
              </div>
            </div>

            <div className="form-group">
              <label for="bankname">Bank Name:</label>
              <input
                type="bankname"
                className="form-control"
                id="bankname"
                onChange={e => actionCreators.updateBankName(e.target.value)}
              />
            </div>

            <div className="form-group">
              <button type="button" className="btn addbank">
                + Add bank account
              </button>
            </div>

            <div className="form-group">
              <button type="button" class="btn btn-success">
                Submit
              </button>
            </div>
          </form>
        </div>
      </div>
    );
  }
}

export default Form;