score:4

Accepted answer

I would recommend react-redux to connect your components 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;

Related Query

More Query from same tag