0

I have a component and function. I have to export that function to use somewhere else.

const MyComponent = () => {
   const [myState, setMyState] = useState();

}

const MyFunction() => {
//I want to access myState here
}

export {MyFunction}
export default MyComponent;
burakarslan
  • 245
  • 2
  • 11
  • 1
    You may want to look at https://reactjs.org/docs/context.html – callback Dec 09 '21 at 08:37
  • Can you describe your use case here? Why can't you just pass the state as an argument to MyFunction? – Brendan Bond Dec 09 '21 at 08:39
  • MyFunction is not called from MyComponent. I will use that function at some other component to update MyComponent's state. – burakarslan Dec 09 '21 at 08:40
  • There are many ways to achieve this. For what I can recall now: [Lift the state up](https://chenglou.github.io/react/docs/lifting-state-up.html), use a hook that returns the state or a content provider like callback mentioned in the above – Joshua Dec 09 '21 at 08:40

0 Answers0