Skip to content
Advertisement

How to import an useState object from another folder

I have a project which created in “Creat React App” and i have an useState array which looks like this:

JavaScript

This array is in another folder inside a component called StateComp, but i also want to load this array in the main folder (App.js for this case).

I tried to just import the State and the setState from the StateComp file into the App.js file, but as i expected, the import itself failed.

Anyone have any idea?

Advertisement

Answer

You can use React Context to manage this or Redux. Here’s an example with React Context:

src/providers/StateProvider.js

JavaScript

src/App.js

JavaScript

src/index.js

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement