How to change props after submit and pass value to another component?

Solution for How to change props after submit and pass value to another component?
is Given Below:

I trying to pass phone number value from first step to two step but my props does not change after submit.
This is my parent first step code :

class Register extends Component {
    render() {
        return (
            <div className={""}>
                <div className="row register-row-edit">
                    <div className={"col-lg-6 col-12"}>
                        <RegisterGif/>
                    </div>

                        <div className={"col-lg-6 col-12 mt-5"}>
                            <RegisterMain props={this.props.values} onChange={this.props.handleChange}/>
                        </div>

                </div>

            </div>
        );
    }
}

export default Register;

This is my RegisterMain component that form in it and pass props on it :

     class RegisterMain extends Component {
    constructor(props) {
        super(props);
        this.onSubmit = this.onSubmit.bind(this)
        this.onChange = this.onChange.bind(this)

    }

    static propTypes = {
        isAuthenticated: PropTypes.bool,
        register: PropTypes.func.isRequired,
    }

    onSubmit = (e) => {
        e.preventDefault();
        const {
            firstName,
            lastName,
            phoneNumber,
            address,
            state,
            city
        } = this.state;
        const newUser = {
            firstName,
            lastName,
            phoneNumber,
            address,
            state,
            city
        };
        this.props.register(newUser);
        console.log(newUser)
    }
    onChange(event) {
        const value = event.target.value;
        this.setState({
            [event.target.name]: value
        })
    }
 
    render() {

        return (
            <div className="register-form-box">
              
                <form className="login-form pt-2 float-right" onSubmit={this.onSubmit} >
                    <Row>
                        <div className={"col-12 col-lg-6"}>

                            <input type={"text"}
                                   placeholder={"Last Name"}
                                   name={"lastName"}
                                   onChange={this.onChange}
                                   autoComplete="on"
                                   value={this.state.lastName}
                                   onBlur={(e) => e.target.placeholder = "Last Name"}
                                   onFocus={(e) => e.target.placeholder = ""}
                                   className={" info-login-input"}/>
                        </div>
                        <div className={"col-12 col-lg-6"}>
                            <input
                                name={"firstName"}
                                type={"text"}
                                placeholder={"First Name"}
                                onChange={this.onChange}
                                autoComplete="on"
                                value={this.state.firstName}
                                onBlur={(e) => e.target.placeholder = "First Name"}
                                onFocus={(e) => e.target.placeholder = ""}
                                className={"info-login-input"}/>
                        </div>
                    </Row>
                    <Row>
                        <div className="col-12">
                            <input type={"text"}
                                   placeholder={"Phone"}
                                   name={"phoneNumber"}
                                   onChange={this.onChange}
                                   autoComplete="on"
                                   value={this.state.phoneNumber}
                                   onBlur={(e) => e.target.placeholder = "Phone"}
                                   onFocus={(e) => e.target.placeholder = ""}
                                   className={" mt-3 info-login-input"}/>
                        </div>
                    </Row>
                    <Row>
                        <div className="col-lg-6 col-12 ">

                            <input type={"text"}
                                   placeholder={"City"}
                                   name={"city"}
                                   onChange={this.onChange}
                                   autoComplete="on"
                                   value={this.state.city}
                                   onBlur={(e) => e.target.placeholder = "City"}
                                   onFocus={(e) => e.target.placeholder = ""}
                                   className={"block mt-3 info-login-input"}/>
                        </div>
                        <div className="col-lg-6 col-12">
                            <input type={"text"}
                                   placeholder={"Province"}
                                   name={"state"}
                                   onChange={this.onChange}
                                   autoComplete="on"
                                   value={this.state.state}
                                   onBlur={(e) => e.target.placeholder = "Province"}
                                   onFocus={(e) => e.target.placeholder = ""}
                                   className={"block mt-3 info-login-input"}/>
                        </div>
                    </Row>
                    <Row>
                        <div className={"col-12 mt-3"}>
                                <textarea name={"address"}
                                          value={this.state.address}
                                          onChange={this.onChange}
                                          autoComplete={"on"}
                                          placeholder={"address"}
                                          onBlur={(e) => e.target.placeholder = "address"}
                                          onFocus={(e) => e.target.placeholder = ""}/>
                        </div>
                    </Row>
                    <Row>
                        <div className={"col-12 d-none"}>
                            <input

                                name={"identityType"}
                                type={"text"}
                                onChange={this.onChange}
                                value={this.state.identityType}
                                className={"info-login-input"}/>
                        </div>
                    </Row>
                 
                    <div className="row justify-content-center my-3 ">
                        <div className="col-12 mx-auto send-register-btn">
                            <button type="submit" >SignUp</button>
                        </div>
                    </div>
                </form>
            </div>
        );
    }
}

const mapStateToProps = (state) => ({
    isAuthenticated: state.isAuthenticated,
    error: state.error,
    user : state.auth,

})
export default connect(mapStateToProps, {register})(RegisterMain);

And this is my route for this two step :

    const InitialState = {
    identityType: "Person",
    firstName: "",
    lastName: "",
    phoneNumber: "",
    state: "",
    city: "",
    address: ""
};

export const RegisterOrg = () => {
    const {path} = useRouteMatch();
    const [formState, setFormState] = useState(InitialState);
    const handleChange = useCallback(values => setFormState(values), []);

    return (
        <Switch>
            <Route path={`${path}/step-one`}>
                <Register values={formState} onChange={handleChange} />
            </Route>
            <Route path={`${path}/step-two`}>
                <CheckCode values={formState} onChange={handleChange} />
            </Route>
        </Switch>
    );
}

Props are InitialState in both component and when I change value of input is success to send but props it doesn’t change to get in last step.

Three things missing in the above code is :

  1. No state inside RegisterMain
  2. you haven’t shared register action, so, I would be providing solution wrt onChange
  3. This line of code is faulty, <RegisterMain props={this.props.values} onChange={this.props.handleChange}/> you are passing onChange and referencing handleChange. to avoid this you could like this <RegisterMain {...this.props} />

I have tried simplifying your problem statement, let me know if it’s help. Here goes the solution.

import React, { Component } from "react";
import { Row } from "react-bootstrap";

class RegisterMain extends Component {
  constructor(props) {
    super(props);
    this.onSubmit = this.onSubmit.bind(this);
    this.onChange = this.onChange.bind(this);
    this.state = {
      lastName: "",
      firstName: "",
      phoneNumber: "",
      address: "",
      state: "",
      city: "",
    };
  }

  onSubmit = (e) => {
    e.preventDefault();
    const {
      firstName,
      lastName,
      phoneNumber,
      address,
      state,
      city,
    } = this.state;
    const newUser = {
      firstName,
      lastName,
      phoneNumber,
      address,
      state,
      city,
    };
    this.props.onChange(newUser);
    console.log(newUser);
  };
  onChange(event) {
    const value = event.target.value;
    this.setState({
      [event.target.name]: value,
    });
  }

  render() {
    return (
      <div className="register-form-box">
        <form className="login-form pt-2 float-right" onSubmit={this.onSubmit}>
          <Row>
            <div className={"col-12 col-lg-6"}>
              <input
                type={"text"}
                placeholder={"Last Name"}
                name={"lastName"}
                onChange={this.onChange}
                autoComplete="on"
                value={this.state.lastName}
                onBlur={(e) => (e.target.placeholder = "Last Name")}
                onFocus={(e) => (e.target.placeholder = "")}
                className={" info-login-input"}
              />
            </div>
            <div className={"col-12 col-lg-6"}>
              <input
                name={"firstName"}
                type={"text"}
                placeholder={"First Name"}
                onChange={this.onChange}
                autoComplete="on"
                value={this.state.firstName}
                onBlur={(e) => (e.target.placeholder = "First Name")}
                onFocus={(e) => (e.target.placeholder = "")}
                className={"info-login-input"}
              />
            </div>
          </Row>
          <Row>
            <div className="col-12">
              <input
                type={"text"}
                placeholder={"Phone"}
                name={"phoneNumber"}
                onChange={this.onChange}
                autoComplete="on"
                value={this.state.phoneNumber}
                onBlur={(e) => (e.target.placeholder = "Phone")}
                onFocus={(e) => (e.target.placeholder = "")}
                className={" mt-3 info-login-input"}
              />
            </div>
          </Row>
          <Row>
            <div className="col-lg-6 col-12 ">
              <input
                type={"text"}
                placeholder={"City"}
                name={"city"}
                onChange={this.onChange}
                autoComplete="on"
                value={this.state.city}
                onBlur={(e) => (e.target.placeholder = "City")}
                onFocus={(e) => (e.target.placeholder = "")}
                className={"block mt-3 info-login-input"}
              />
            </div>
            <div className="col-lg-6 col-12">
              <input
                type={"text"}
                placeholder={"Province"}
                name={"state"}
                onChange={this.onChange}
                autoComplete="on"
                value={this.state.state}
                onBlur={(e) => (e.target.placeholder = "Province")}
                onFocus={(e) => (e.target.placeholder = "")}
                className={"block mt-3 info-login-input"}
              />
            </div>
          </Row>
          <Row>
            <div className={"col-12 mt-3"}>
              <textarea
                name={"address"}
                value={this.state.address}
                onChange={this.onChange}
                autoComplete={"on"}
                placeholder={"address"}
                onBlur={(e) => (e.target.placeholder = "address")}
                onFocus={(e) => (e.target.placeholder = "")}
              />
            </div>
          </Row>
          <Row>
            <div className={"col-12 d-none"}>
              <input
                name={"identityType"}
                type={"text"}
                onChange={this.onChange}
                value={this.state.identityType}
                className={"info-login-input"}
              />
            </div>
          </Row>

          <div className="row justify-content-center my-3 ">
            <div className="col-12 mx-auto send-register-btn">
              <button type="submit">SignUp</button>
            </div>
          </div>
        </form>
      </div>
    );
  }
}

export default RegisterMain;
import React, { Component, useCallback, useState } from "react";
import RegisterMain from "./RegisterMain";

class Register extends Component {
  render() {
    return (
      <div className={""}>
        <div className="row register-row-edit">
          <div className={"col-lg-6 col-12"}>{/* <RegisterGif/> */}test</div>

          <div className={"col-lg-6 col-12 mt-5"}>
            <RegisterMain {...this.props} />
          </div>
        </div>
      </div>
    );
  }
}

export default Register;

const InitialState = {
  identityType: "Person",
  firstName: "",
  lastName: "",
  phoneNumber: "",
  state: "",
  city: "",
  address: "",
};

export const RegisterOrg = () => {
  //   const { path } = useRouteMatch();
  const [formState, setFormState] = useState(InitialState);
  console.log(formState);
  const handleChange = useCallback((values) => setFormState(values), []);

  return <Register values={formState} onChange={handleChange} />;
};