7

I am very new to react and redux.I am trying to pass data from one component to another. but it has no parent child relation and it is independent from each other. can anyone help me to do this? below are my code.

export class EmpSearch extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      Empnumber: ''
    };
  }

  updateEmpNumber(e) {
    this.setState({Empnumber: e.target.value});
  }

  render() {
    return (
      <div className="row">
        <form>
          <div className="form-group">
            <label htmlFor="Empnumber">Emp Number</label>
            <input type="text" className="form-control" id="Empnumber" placeholder="Emp Number" value={this.state.Empnumber} onChange={this.updateEmpNumber.bind(this)}/>
          </div>
        </form>
      </div>
    );
  }
}

function mapStateToProps(state){
  return{
    Empnumber: state.Empnumber
  };
}

export default connect(mapStateToProps)(EmpSearch);

The other file is where i want to send the EmpNumber is below,

class EmpDetail extends React.Component {
  render() {
    return (
      <div className="container">
        <input type="text"/>
      </div>
    );
  }
}

export default EmpDetail;
11
  • 1
    Have you created reducer function? if not then you have to create a reducer function and dispatch an action updateEmpNumber function. then you can access EmpNumber from redux store. Commented Mar 3, 2017 at 6:26
  • No i have not created that because i have no idea how to create it. It will be very helpful if can help me out. Commented Mar 3, 2017 at 6:27
  • I am not sure how to dispatch and action and how to create a reducer. Commented Mar 3, 2017 at 6:28
  • Here is the basic ToDo List example, go through this tutorial and see if you can create it yourself. Commented Mar 3, 2017 at 6:34
  • 2
    Ah ok sorry i re-read this. What i said is correct (in that connect will allow you to get EmpNumber from redux store) but as others mention, you need to create a 'reducer' and dispatch an action to update the EmpNumber value in the redux store. I dont know your architecture so its hard to give an example, best to just try and get your head around he basic examples here...redux.js.org/docs/basics/Actions.html and redux.js.org/docs/basics/Reducers.html Commented Mar 3, 2017 at 6:51

1 Answer 1

20

Ok so here is an example. I'm going to assume you have all the Redux store configured already, i.e. store created, rootReducer etc... as that's a separate post altogether.

So the basic idea is that when you want to update your Redux store state you dispatch an action to the reducer and send a payload of data along with it. This sounds confusing but essentially it's just to tell the reducer what you want to do i.e. "Hey reducer I want you to UPDATE_EMP_NUMBER and here is the data".

A reducer is basically just a massive switch statement as per below, that simply returns the new part of state based on your given action.

Again I know nothing about your architecture, store etc so this is all assumption based but here is what it may look like for you.

Reducer

export default function employeeReducer(state = [], action) {
    switch (action.type) {
        case UPDATE_EMP_NUMBER:
            {
                const Empnumber = action.payload;
                return {
                    ...state,
                    Empnumber
                };
            }
            case ANOTHER_ACTION:
            {
                // etc...
            }
        default:
            return state;
    }
}

With the above in mind, you could update your code like so.

EmpSearch

export class EmpSearch extends React.Component {
        // Not needed anymore as state going to Redux and not local component state
    /*
        constructor(props) {
        super(props);
        this.state = {
            Empnumber: ''
        };
        }
    */

    updateEmpNumber(e) {
        // No need to set state here as you are now passing this data to Redux store
        // this.setState({Empnumber: e.target.value});

        // Instead we will dispatch an action and send the empNumber to the reducer
        this.props.dispatch({
                type: 'UPDATE_EMP_NUMBER',
                payload: e.target.value
            });
    }

    render() {
    return (
        <div className="row">
        <form>
            <div className="form-group">
            <label htmlFor="Empnumber">Emp Number</label>
            <input type="text" className="form-control" id="Empnumber" placeholder="Emp Number" value={this.props.Empnumber} onChange={this.updateEmpNumber.bind(this)}/>
            </div>
        </form>
        </div>
    );
    }
}
    
function mapStateToProps(state){
    return {
        Empnumber: state.Empnumber
    }
}
    
export default connect(mapStateToProps)(EmpSearch);

Then your EmpDetail class just needs to connect to the store (or be passed the data as a prop, but easier to just connect it).

EmpDetail

class EmpDetail extends React.Component {
    render() {
        const empNumber = this.props.Empnumber;
        return (
            <div className="container">
                Empnumber = {empNumber}
            </div>
        );
    }
}

function mapStateToProps(state){
    return {
        Empnumber: state.Empnumber
    }
}
    
export default connect(mapStateToProps)(EmpDetail);

I really hope this helps, it's so hard to explain this when you can't see where this is going to end up!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I really appreciate it. I did the same as you mentioned above but in browser console i am getting below error. Uncaught Error: Could not find "store" in either the context or props of "Connect(EmpSearch)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(EmpSearch)".
able to figure out. It is working perfectly fine... Thanks a Lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.