Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set React component state from outside of component

Tags:

I have a list of components and want to set each component as selected when the user clicks on it.

The flow looks like

Dashboard  ⎿ MyList     ⎿ MyItem -> onClick -> setState({active:true}) 

I've got the selected part done by using state but I'm left wondering how to deactivate all the other elements.

like image 834
Eldelshell Avatar asked May 29 '16 18:05

Eldelshell


People also ask

How do you set a state from another component React?

You can't directly call setState on a parent component from a child component because the updating of a component state is restricted to the current component. To handle this, simply pass a function from the parent to the child that contains setState .

Can we use state outside component?

no, you can't use useState outside of a functional component.

Can you use setState outside component?

Short answer: No, you cannot setState outside a component.

Can set state on unmounted component?

Seeing called setState() on an unmounted component in your browser console means the callback for an async operation is still running after a component's removed from the DOM. This points to a memory leak caused by doing redundant work which the user will never benefit from.


2 Answers

By definition, state is not accessible from outside the component. And always copying props to state is not recommended.

In your component tree structure. you should set the selected item as state in the parent (not in the item).

And pass the selected Id to each item as a prop.

In the child render, you can do something like

let itemIsSelected = (this.props.itemId == this.props.selectedId); 

And pass a method from parent to child, and then include that as:

onClick={() => this.props.setSelected(this.props.itemId)} 

In the official docs, there is a good explanation on how to structure components. This may be helpful to determine whether something should be state or props, or whether stuff is better managed inside child or parent.

like image 50
wintvelt Avatar answered Sep 19 '22 22:09

wintvelt


Dan Abramov mentions another clever way to access a state change from outside a component class. Technically the setState function still has to be called from within the component class anyway, so it's not REALLY outside the component, but this allows you to set the state of different components in the same fashion if you want, from the returned value of a function.

Basically, you can pass in a function into the setState() function, and the state will be set to the returned value of that function.

you can see his post here: https://twitter.com/dan_abramov/status/824308413559668744?lang=en

like image 42
xunux Avatar answered Sep 18 '22 22:09

xunux