0

I am not an expert in react. My problem statement is that

  • I have a main component which in turn renders multiple child components for specific functions
  • Now, when I save the main component , I want to save information from child components also.

enter image description here

What is the correct way to deal with this?

  1. Using callbacks in child components that return state of child?
  2. redux store/ useContext hook? Something else?

Would be great to get some advice. Thanks in advance

Nikhil
  • 31
  • 3
  • What do you mean by saving the information? If you want to save the state of the application, it is always easier and advisable to do so using a global state or at least a top-level state (Using context). The answer may depend on your use case though. – Afridi Kayal Jul 02 '23 at 07:51

1 Answers1

0

Generally this is done by "lifting the state up", so one solution for this would be managing all of the state (including children components state) in the main component and passing down the state in props and functions which trigger the save.

This general concept can be applied also with "grouping" the state into some meaningful object in redux store or react context.

Some related questions:

Kryštof Řeháček
  • 1,965
  • 1
  • 16
  • 28