score:12
When you want 2 children to communicate or share some data, the way to do it in React is to lift state up (source).
This means that the state that the children use should live in the parent. Then the parent passes the state down to the children.
To update the state from an action in a child, the usual pattern is to pass down a function from the parent that gets called when the action is performed in the child.
Here's an example that should do what you want:
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = { valueOfUserInput: '' };
}
handleUserInputChange = event => {
this.setState({
valueOfUserInput: event.target.value,
});
};
render() {
const { valueOfUserInput } = this.state;
return (
<div>
<Child1 valueOfUserInput={valueOfUserInput} onUserInputChange={this.handleUserInputChange} />
<Child2 valueOfUserInput={valueOfUserInput} />
</div>
);
}
}
class Child1 extends React.Component {
render() {
const { valueOfUserInput, onUserInputChange } = this.props;
return <input type="text" value={valueOfUserInput} onChange={onUserInputChange} />;
}
}
class Child2 extends React.Component {
render() {
const { valueOfUserInput } = this.props;
return (
<div>
{valueOfUserInput}
</div>
);
}
}
score:0
You can pass a function down from the parent to both of the children, to allow setting of the value. You may want to pass the value as another property. The function in the parent should make a call to setState with this new value, which will cause re-rendering of itself and any children.
So in this way the value is updated and passed to the other child.
You can also do this if you are using redux, as it is designed to allow state management outside of a component.
score:2
Your diagram sort of makes sense, and I think I know the key piece that you're missing. The idea of Child 1
passing the prop up to the parent is partially correct... but how you pass that prop is that you must pass a function down to Child 1
from parent. So in parent, you'd have something like...
handleChange(val) {
this.setState({ blah: val }) // you are going to pass blah down to Child 2
}
and then in your render function in Parent
, you're passing this function down...
<Child 1 onChange={this.handleChange} />
Inside of `Child 1, you need to call that function.
<input onChange={this.props.onChange(val)} />
Child 1
now goes "hey, Parent, I've been changed, and I got some value here, you do the rest". Parent
handles the event by setting its state, and then it can pass it down to whoever. Hopefully that makes sense, but this should be a helpful start.
score:2
I would personally use react-redux to do this communication. Redux, on a high level, is a global state manager. You can modify and access the reducer values from any of your components. Feel free to take a look at the boilerplate I made: https://github.com/rjzheng/REWBoilerplate/. If you look under '/app', you can see how everything is set up in the '/reducer' and '/store' folders. To learn how to use them, there's a pretty comprehensive documentation on it at http://redux.js.org/docs/basics/UsageWithReact.html.
Source: stackoverflow.com
Related Query
- React: how to pass props from child to parent to another child?
- React TS - How to pass props from a parent component to a deeply nested child
- React & TypeScript how pass data from child to parent at onClick via props
- How to pass props or state from parent to child react in this format
- how to pass props from one component to other in react router that don't have parent child relationship?
- How do I pass an object (created by a buttonClick event) from child to parent in react when already using props for a styled button
- How to pass React props from parent to child using composition pattern
- How do I pass props from Child to Parent in React for incremental onClick?
- how to pass an event from a child component to parent component back down to another child in React
- How to use onClick event to pass the props from child component to parent component React Hooks
- React - How to pass `ref` from child to parent component?
- How to pass function as props from functional parent component to child
- How to pass input value from child to parent component react
- How to pass props of one child component to another child component using parent component
- How to pass an Array from Child component to Parent component in react I know how to pass from parent t
- How to pass form values from child component to parent in react
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to pass props props from Parent to this.props.children in React
- Pass data from child to parent and to another child component React Js
- How to pass a prop back from child to parent in onclick handler and then to another component in React?
- react pass props from child back to parent
- How to pass props from child component to its parent of parent?
- How to pass handlSubmit and handleChange functions from Parent to Child in React
- how to pass in a value from child to parent class in react js
- How can I pass image url from parent component to child component via props [REACT.JS]
- How can I pass props from a Parent to Child with this.props.children?
- How to pass state from child to parent by right way in React
- How to get parent props from child composition component React
- Nextjs dynamic routes - how to pass props from parent to dynamic route child
- How to pass latest state to a Parent component from Child Component in react
More Query from same tag
- How to automate Reactjs drop downs using Watir
- How to create an asynchronous function to request data in reactjs?
- Google Firebase Auth Failed on Netlify, But work on local server
- ReactJS App doesn't update data without refreshing browser
- How to debounce mui Autocomplete
- Spring react - operator =>
- Filtering API response with JavaScript/React
- use of React.Lazy for services (non-component) type of code
- First steps in Redux. How to add value to mapStateToProps?
- Header component doesn't get updated after route change
- React: How to modify an array-based state, without an index or a unique identifier?
- Hooks equivalent for componentWilLReceiveProps to update state
- React Hooks add/subtract count from click div/list
- React page gives back 404 page after refresh
- Is there a way to declare styled component with different main elements?
- The correct way to update props with Mapbox
- While deleting an element from an object, its deleting from the parent in react js
- Typescript and React (with Gatsby loader): unable to import images as modules
- React.js css box-shadow not working
- passing json information to child
- How to connect backend create user function to front end (Node.js, React.js, MongoDB)
- configure React (build) routes.js with Django urls.py
- _this.props.removeFromFavs is not a function
- Multiple Axios GET requests from different components
- Which is better CRUD coding style using Redux?
- ReactJS: Global Okta Auth Helper Function
- Handling Null Object Property Value in Component
- How to test React useEffect hooks with jasmine and enzyme
- How to pass useRef props to two components to focus an input ( textarea ) - react.js and typescript
- reactJS remove Items from an array