2

Currently I am using reat creat app to build my appication in my application had three componentes those are com1, com2, com3 I want to update userId state value in com1 based on com3 will recive props here com2 is child component of com1.

Here is my sample code

import comp2 from './comp2.js';
class comp1 extends React.Component {
    constructor(props) {
        super(props);

          this.state = {
          userId:""

         }
        };

    click() {
        this.setSate({userId:"123"});
    }

    render() {
          <div>Hello Child onClick={this.click}</>
          <comp2 data={this.state.userId}
    }
}

import comp3 from './comp3.js';
class comp2 extends React.Component {
    constructor(props) {
        super(props);
        };

        componentWillReceiveProps(nextProps) {
        if (nextProps.data !== this.props.data) {
         this.setState({userID:this.state.userId});
       }
 }
}


    click() {
         this.setState({userID:"456"})
    }

    render() {
          <div>Hello Child onClick={this.click}</>
          <comp3 data={this.state.userId}
    }
}

class comp3 extends React.Component {
    constructor(props) {
        super(props);
        };

    componentWillReceiveProps(nextProps) {
        if (nextProps.data !== this.props.data) {
         this.setState({userID:this.state.userId});
       }
 }

    render() {
          <div>Hello Child onClick={this.click}</>
          <comp3 data={this.state.userId}
    }
}
2
  • 1
    comp3 has comp3 itself as a child?? Commented Jun 10, 2017 at 12:53
  • comp3 is child component of comp2 Commented Jun 12, 2017 at 4:10

1 Answer 1

0

You can use shared store between them or the parent dhould to manage data between children.

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

Comments

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.