0

I have 3 components, that is 2 child components and one parent component. I wanted to pass the child component value to parent (only the values not the components enitirely,it should not visible in parent) and this values from parent to another child component.

Any suggestions or logic of how to proceed on this, since I don't have any guidance as of right now I had to ask here. Is the above problem possible.

The code is very complex, so I have not put here. Thank you

1
  • 1
    You can use redux to manage app state. Commented Mar 2, 2018 at 4:46

4 Answers 4

1

When you say values, do you mean state, props, user input, something else?

If you mean state or props: React has a 1-way data flow, so the easiest way to accomplish this is to actually store the data at a higher level. Either store the data used by the child in the parent and pass it down to the children for consumption, or else use a store that both parent and children have access to. Either way, this will make it much easier for all components to access the data.

If you mean user input: one way you can accomplish this is to pass a callback from the parent component to the child as a prop, and then in the child call that callback when a user does something or changes some value. The callback function can make the data accessible to the parent on that user action, and then you can decide what to do with the data from there.

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

Comments

1

AksharaDL,

Child to Parent — Use a callback and states

Parent to Child — Use Prop

Also here is another article explaining it: https://medium.com/@ruthmpardee/passing-data-between-react-components-103ad82ebd17

1 Comment

You can also check out the following codepen to give you an example: codepen.io/PiotrBerebecki/pen/dpRdKP
1

here is the solution. in parrent component you have a state. and have a setData method to update state. pass setData to ChildOne use props. and data to ChilTwo and use it

class StoreBanner extends React.Component {
  constructor() {
    this.state = {
      data: 'whatever'
    }
  }

  setData = (data) => {
    this.setState({data})
  }

  render() {
    return (
      <div>
        <ChildOne setData={this.setData}/>
        <ChildTwo data={this.state.data}/>
      </div>
    )
  }
}

and in ChildOne you can update the parrent state

class ChildOne extends React.Component {
  setParentData = () => {
    this.props.setData('another')
  }
  ...
}

2 Comments

ChildOne component should not display in parent, can we pass only values?
If you want do this you can use Redux
0

You can do it like this.

class ParentComp extends React.Component {
    constructor() {
        super();

        this.state = {
            theStateToPass: null
        }
        this.receiveDataFromChild = this.receiveDataFromChild.bind(this);
    }

    receiveDataFromChild(data) {
        this.setState({
            theStateToPass: data
        })
    }
    render() {
        return (
            <div>
                <FirstChild updateMe={this.receiveDataFromChild} />
                <SecondChild stateFromFirstChild={this.state.theStateToPass} />
            </div>
        )
    }
}

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

        this.callParentMethod = this.callParentMethod.bind(this);
    }

    callParentMethod(e) {
        let someDataToSend = "ABC";
        this.props.updateMe(someDataToSend)
    }
    render() {
        return (
            <div onClick={this.callParentMethod}>

            </div>
        )
    }
}

class SecondChild extends React.Component {
    render() {
        return (
            <div>
                {this.props.stateFromFirstChild}
            </div>
        )
    }
}

however it becomes complex and might lead to one's pulling their hair out. so i would suggest using redux , it keeps the flow simple , you have a reducer , actions and a container. everything goes with a flow and keeps it clean but it does comes with an extra overhead of more code as you will be creating container reducer and actions.

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.