Component child with two parents, - reactjs

im newbie on Ractsjs, and now i made on course on Plural sight, the situation its the fallowing, i have one component(child) who is the son and in one hand i have a one component2(father) thats send the 'props' for the component3(son) use them; and for other hand, this component3(son) wants send a callback function to the other component1(father),
in resthat´s my schema:
Component1 Component3
|with callbaclk | with props
| |
└ Component3 ┘
::The seem child receive 'props' from on component(father1) and this seem child component need send a callback function to the other component(father2).........how i can do it??
thks in advance
pd) i attached my code
https://github.com/jmonteros81/Building_-Applications_React
enter image description here

Callbacks are used to send data from child to parent. Callbacks are just simple functions. So you have to write a callback in your parent component and Call that callback from your Child component from where you want to send data to parent.
In Parent component define a function.
getAndPassValue(value) {
this.setState({ count: value });
}
in render pass this function to your child
<ThirdComponent getAndPassValue={this.getAndPassValue} />
In Child component from where you want to pass data to parent call this function.
<button onClick={() => this.props.passvalue(5)} />
Now pass this count to your first Child component from parent
Now you can access this count in your FirstChild component using this.props.count and you can get 5 value passed from your third component.

Related

How to use refs when parent and child component are both functional component?

I have two components, one being Parent and other one Child, both as functional components. In the parent component I have a submit button, which when invoked will call a function defined in the Child functional component (e.g. submitHandler()). By using the forwardRef() in child component I am unable to access the submitHandler() of child functional component. Any idea/help regarding how can i tackle this?
You are looking for useImperativeHandle hook.
for more info have a look at this post
Call child method from parent

Access data injected into grandchild component

The grandchild component is wrapped in a container which receives data via a graphql query.
I need to access that data from the Grandparent component in order to check the values in the data set to perform some functionality .
How can I read what data the grandchild has received?
Pass a function from grand parent to the grand child component (you may have to pass it two levels deep, e.g. via Child), then
the grand child will invoke that function (because it received it from props) and pass the data it wants as parameter - hence grand parent will have chance to respond.
Rough sketch:
class GrandParent{
// This function will get called when grand child receives data
dataReceivedInGrandChild(data){
//Do whatever you want with data
}
render(){
....
<Child dataReceivedInGrandChild={this.dataReceivedInGrandChild}/>
....
}
}
you can make a function within the grandchild to return the desired data, then add refs to the parent and to the grandchild and in the grandparent you have to refer to these refs e.g.:
grandP.parent.grandC.getData
or you can fetch the data within the grandparent and path it via props to child and to grandchild

The Change in value of prop doesn't make the child component re render

I have a parent component which passdown an array prop to a child component. but as render runs before componentDidMount ,where I am setting the value to that array.. the child component recieve an empty array as prop.
I searched on net ,it seems componentWillRecieveProps Can help wiyh making child component to rr render.. but as this method is deprecated in react 16 and further. What should be the goto way of doing it.
Only call the child component when the props value has been populated like
this.state.arrayProp.length && <ChildComponent />
Now when you have populated arrayProp array state in componentDidMount, render method will again be called after componentDidMount, So, now ChildComponent will be called with correctly populated array value.
Hope that helps!!!

REACT: how tell parent component about invalid data from child?

I have a child component that renders a list (after processing some complex logic) that is passed to it from parent using props
Problem
If parent passes an invalid list then child renders nothing and parent shows a empty li
I want to check if child has not rendered any thing so that li can be removed.
Introducing new dependency (noticing parent) won't help in reusability.
You shouldn't provide bad data to components - filter them before passing down.
Obvious answer is to not render li externally - render it inside a child.
To preserve reusability pass this <li /> as a 'tag' or 'component' prop to child and render conditionally (decorate content) when defined. It's quite common pattern in react. You can also use render props pattern.

Mutate state of children

I have two components in my react application, one is parent and the second is the child.
I pass fakedata state as a props to child and then in child component save this as state of child component. But when I change something in child it's affect on parent state and I don't want this I want it's only effect on child state.
This is how I call child component from parent :
<FakeDataAddEditComponent {...this.props} fakedata={{...this.state.fakedata}} />
and in child component this is how I set fakedata props to state :
componentDidMount() {
this.setState({fakedata:{...this.props.fakedata}},()=>{
})
}
but when I change fakedata state in child it's also changend in fakedata of parent and I don't want this.
To answer your question directly, the reason you are seeing this behaviour is because Javascript passes a reference to the original object down the props and to the state of child component. When you update the child component it is the same instance as the parent is holding.
To fix the problem you should use Object.assign to make a copy of the object, however keep in ming that you will run into problems with nested objects.
Also, if parent object mutates the state in any way after the child state changed it will pass old object as props to the child.
In general you're trying to do something you shouldn't do because you will run into trouble.
On a high level you should go for a proper state management solution like Redux of Flux.

Resources