Search code examples
reactjscomponentsparent-childreact-propsreact-context

How can I pass data between two React components, but the two components have no parent-child relationship?


For example, I have data in a component which is inside "components" folder. Now I want to use the data in an another component which is inside "views" folder. So far I know, Context API will not work here. So how can I pass data in this scenario? Thanks in advance.

enter image description here


Solution

  • Use Redux by maintaining the states of all child components that you might need in a global store and obtain the data from said store.

    https://react-redux.js.org/introduction/getting-started

    make sure to use redux toolkit to simplify your setup: https://redux-toolkit.js.org/introduction/getting-started