I want to update the Diagram component if I click on the Button.
I have the following structure:
+------+
-Layout|--\
-/+------+ |
-/ \
+-----/---+ +----|----+
| Sidebar | | Content |
+--|------+ +----|----+
/ \
+---|---+ +---|----+
|Button | |Diagram |
+-------+ +--------+
If I press the Button, the Diagram should update. My only idea how I can do this is the following:
changeDiagram() function in the Layout component.
type to the value 'barChart'changeDiagram as a parameter to sidebar. If i click on the button the changeDiagram function gets excecuted.type variable that is in the Layout component as a parameter to the Content component.The problem is that I don't want to define a function that is only responsible for the Diagram inside the Layout Component. Also every Component between Layout and Diagram gets rendered, which is not necessary.
How to solve this problem? I heard of react-redux. Do I have to use this or are there other solutions?
The answer to this question is deeply linked to the very inner nature of React:
With this premise, you understand that if you want your Button to trigger a rerender of Diagram, you need Button to change a React State held in Layout ( which is the lower common Parent ) or in a Component that is a Parent to Layout. This is why React re-renders with a top-down flow, so for example, a State change in Sidebar, will re-render only Sidebar and Button and not Layout.
How to solve this problem? I heard of react-redux. Do I have to use this or are there other solutions?
So, this is up to you, if you plan to have a lot of these scenarios yes, you probably need a state manager, Redux, Recoil, Valtio, Zustand there are many of them. What fits better your case depends from many factors just try them and see which one you like more.
The only solution without redux (or context API) is there
If you want to only render needed component you'll have to use redux and its store. OR you'll have to use Context API