I know that hooks cannot be called from functions other than React Functional Components, this is a bit different.
I have created some utility/service type functions that need to use hooks.
// somewhere in services files...
const utilNavTo = (route) => {
const navigate = useNavigate();
...
};
// somewhere in components for screens...
const MyScreen = () => {
...
if(something){
utilNavTo('/somewhere');
}
...
};
// the main app is created with <App /> not App, so that should not be the reason of error
ReactDOM.render(<App />, document.getElementById("root"));
// also App is fully defined with routes having <MyScreen /> defined properly...
When such a function is used within a React Functional Component, I get this error:
Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
If I call the hook from the react functional component and pass it to the utility function, it works perfectly well.
Is there some way to make this work without passing the hooks as arguments? I guess it should only fail if the utility function itself is not called from a functional component.