React send new props and then clear state - javascript

I have the following situation:
I have a parent React component and a child.
The parent has a table and the child has some controls of its own.
What I want to accomplish is being able to click on a cell in the parent table and have the cell value get transferred over to the child so that the user can edit it there.
This is what I implemented:
I record the cell value in state in the parent on click.
When this I record this value, a re-render happens and the string gets sent to the child via props.
The issue I'm running into is that if something forces a re-render on the parent, that value will get sent to the child again, even though the child may have changed it.
To solve this, what I thought of originally was to send the prop down to the child and then have the child reach back up via a callback to the parent to clear the value in the parent's state. I fall into a loop doing this so naturally it doesn't work.
What is the correct way to send data down into a child component and then have it get cleared post-render?

Each React component update will dispatch a re render action. And when a child component receive new props that component will to re render too.
Note that a child item should be re render only if the props values are news o the inner component state have changed.
For your situation I say: you can pass a object to your child component using props, and execute an function in the parent inside of the child. You only need pass the function in the parent to the child component as a prop, after, execute it inside the child component when you need it.

State that is passed down to children via props should be modified by the parent. I would look into 'react lifting state'
Class Component:
But essentially, your child's handlers (ex. handleClick) should be declared in your parent component and passed down to your child component as an onHandler (ex. onClick=this.onHandleClick) and in the child can be referenced as this.props.onClick
Functional Component:
Basically the same thing but you shouldn't use the this. reference.

Related

How to use child component data in parent component? How to make a binding?

For example, we have one field Input, property 'currentNode' in child component and property 'activeElement' in parent component. Every time we change value in Input, it affects property 'currentNode'. And also, of course, parent component contains child component (I mean HTML).
So, I want that every time currentNode changes somehow, it affected activeElement, which's located in parent component.
Add (change)="" method into your Input field. Then Use #Output() to send the event on every change. While you emit currentNode in your parent. your parent function automatically calls then you can update your parent activeElement with the emitting value
click here to see How you can send data to your parent component

Passing state from child to parent

Hi I'm just starting with React and I came across a problem with passing states. Can I pass child state to a parent component to conditionally render other child components?
In React, you cannot pass state to a parent component. Instead, notify the parent component of changes using a callback function.
To do so, pass a function (defined in the parent) in the props of the child component, and call it when changes happen. The parent can then notify (other) child components through their props.
Passing state from child to parent is remarkably easy - all you need to do is pass a function from the parent to the child which receives the state.
getState=(childState)=>{
//use state here
}
<>
<customChild returnState={this.getState} />
<>
class customChild extends.... {
...
render{
<button onClick={this.props.returnState(this.state)}>Click Me!</button>
}
}
That being said, ideally you want the parent to control all the state values and pass just the needed values down to the child. As the parent state changes the child will re-render that changed items. This way your entire page has its state(s) all in a single location for debugging.

React: If children is a part of props, then why doesn't it re-render the parent

A React component re-renders when its props change. Ok.
But its children are passed through props: the children prop, most of the time.
So: when a child is re-rendered, because its state changed for example (it could be triggered by a change event), the children changes, doesn't it? If it changes, the prop.children changes too. If prop.children changes, the component should re-render. And the same for its parent and all its ancestors!
So the tiniest "leaf" component change should trigger all the component tree to re-render! But it does not, and I don't get why...
Could anyone bring some light?

Update/refresh a component using another component

I'm sorry for this question but I'm new to react and trying to find best practice on how to update another class component from another class component.
For example I have AddItem.js and ViewItem.js but has the same route. I'm updating ViewItem.js ListGroup by re-rending(componentDidMount()). How can I do this with AddItem.js button onClick?
You can take a new state property in the parent and pass that as props to AddItem and ViewItem and use that prop as a state for these two components and in your button click event you can change the state and react renders the component automatically on state change.
See this simple example
If ViewItem and AddItem are siblings, then you can call methods of each other using ref of another component use ref doc
OR
you can also wrap both with parent component and use parent state for both the child.
If Viewitem and Additem are child-parent or vice versa then you can use pass props and change state based on the change in props.
It can be achieved by creating a parent class (for example Home.js) on top of AddItem.js and ViewItem.js and pass data as props on click to the child component.
In case you if you are using React and Redux it becomes relatively simple on click of a button you can call method in your action.js which will dispatch an action with payload data to the reducer, which can we used by adding mapStatetoProps() in ViewItem.js and AddItem.js, so once you receive the data in your props variables it will reload/refresh that particular component.

Call a sub-component method from parent compoent in Angular 2 +

I'm new to Angular, and as the title states, I'm trying to see if this is doable.
Instead of using the standard way of a sub-component sending data via it's #Output EventEmitter, I would like to have the same concept (without injecting the parent into the child) but with the parent component emitting data to a child. Can a parent component emit data to a child? If not, why is that so?
Just to clarify, the sub-component is a form that gets filled in. As soon as the form is complete I want a button on the parent component to see if the form was complete before submitting it.
This was achieved simply by interacting with the child component using a template reference variable.
Add the variable to the child component selector element and access its properties and methods with it from the parent template:
<child-selector #variable ></child-selector>
<button (click)="variable.ChildMethod()"/>
Alternatively, access the child component method/properties directly from component class:
<button (click)="AccessChildMemebers(variable)"/>
AccessChildMemebers(elem){
elem.ChildMethod();
}

Categories

Resources