The file Editor.js in the components directory contains this
const Editor = () => {
//some states
const [mathEditorVisible, setMathEditorVisible] = useState(false);
//some other code
}
This is Form.js in the utils directory:
//some imports
export const handleSaveFormula = (latex) => {
//some code
setMathEditorVisible(false);
};
This is Editor.js in the utils directory:
//some exports
export const editExpression = () => {
//some code
setMathEditorVisible(true);
};
Now, I want to import the setMathEditorVisible from Editor.js in the components directory where it is defined to the Editor.js and Form.js files in the utils directory.
This error occurs if I do not import it
**ERROR in [eslint]
src/utils/Editor.js
Line 17:8: ‘setMathEditorVisible’ is not defined no-undef
src/utils/Form.js
Line 49:7: ‘setMathEditorVisible’ is not defined no-undef
**
And of course it will because it is undefined for now.
2
Answers
u can move the state to component alone and import it
now u can impoet it in any component
You’ll have to manage this without imports. Since the state setter is just a local variable in a component function.
However, fear not, this problem can be solved in a couple other ways:
Parameters
You can pass
mathEditorVisible
andsetMathEditorVisible
intoEditor
and into yourhandleSaveFormula
andeditExpression
functions from component of a higher level (e.g. page).It will look something like this:
Context
Another way to do this is put your variables into context (which would have to be on top of Editor component and the functions you’re calling
setMathEditorVisible
in).Context lets you access its values in any (even nested) child components.
So, in this situation you would store your
[mathEditorVisible, setMathEditorVisible]
invalue
property of a context component (ContextObject.Provider
) created byReact.createContext
. Then later you can access the values throughuseContext
hook, passing the original context object as the argument.Here’s the example:
Note that with context, since you have to call useContext hook, you would need to retrieve values in a component, and if needed, pass it to underlying functions.
Conclusion
As some other people already mentioned, you’re probably better off with the first approach. However, it’s good to know how context works, in case you need to access something in a lot unconnected components/places.