javascript - How to take input value from submit form and store in redux store variable? - Stack Overflow

I have made a bank account submit form. I want to save the data which is entered in the form into redux

I have made a bank account submit form. I want to save the data which is entered in the form into redux store. How can I take input value from form and store it in redux store variable ?

My client.js file has redux store and form.js is the ponent from which I need to get the input values. client.js:

import { bineReducers, 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 = bineReducers({
    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:"[email protected]"});
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;

Screenshot of my form:

I have made a bank account submit form. I want to save the data which is entered in the form into redux store. How can I take input value from form and store it in redux store variable ?

My client.js file has redux store and form.js is the ponent from which I need to get the input values. client.js:

import { bineReducers, 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 = bineReducers({
    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:"[email protected]"});
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;

Screenshot of my form:

Share Improve this question edited May 27, 2018 at 7:44 Tomasz Mularczyk 36.3k19 gold badges118 silver badges174 bronze badges asked May 27, 2018 at 7:31 stone rockstone rock 1,95310 gold badges45 silver badges76 bronze badges 5
  • do you use react-redux? – Tomasz Mularczyk Commented May 27, 2018 at 7:33
  • @TomaszMularczyk Sorry I have removed that tag. – stone rock Commented May 27, 2018 at 7:34
  • it would be best if you could use it though – Tomasz Mularczyk Commented May 27, 2018 at 7:36
  • @TomaszMularczyk How can I take input from submit form and store info about particular user on submitting the form. How can I implement it using redux and react. – stone rock Commented May 27, 2018 at 7:39
  • How did you achieve this I am supposed to do exactly the same. Please help. I do not want to use action creators and I want to do this using react and react-redux. – cooler Commented May 24, 2019 at 7:58
Add a ment  | 

1 Answer 1

Reset to default 4

I would remend react-redux to connect your ponents with redux store, however it is still doable without it:

Create action creators that will update specific variable in the store:

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
});

Now bind your action creators with dispatch, so calling actionCreators.updateFirsName('something') will actually dispatch an action to the store.

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

Now you only need to call each store-updating function whenever theres an change on the input:

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745628972a4636986.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信