Search code examples
javascriptreactjscomponents

How to import the setState function in reactjs


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

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.


Solution

  • I just passed the state as an argument to the functions where I need to manage it and it works as expected, thank you guys for your consideration!