Samurai Jack
Samurai Jack

Reputation: 3125

State of states in React

I've searched about this but didn't find anything related.

Is it possible to have in React a state that has also other states?

For example, if there is a list of states and each of these states when is activated can also have other lists of states? If yes, which is the approach to do this?

Upvotes: 1

Views: 57

Answers (1)

Amid
Amid

Reputation: 22332

Yes it is possible. Technically state is a simple javascript object and there is nothing that prohibit your from nesting objects and using each as a state for corresponding component.

You can store state of the child components outside in the parent component state and then pass them down to children in props. Each child will respectively use this object to set as its state in constructor.

Upvotes: 1

Related Questions