I’m curious if i could do something like this:
const [panelActive, setPanelActive] = useState(false);
(globalThis as any).setPanelActive = setPanelActive;
useEffect(() => console.log(panelActive), [panelActive])
For some reason useEffect is not getting triggered when i call setPanelActive
function from outside. Should i create some sort of wrapper or context provider in order to make it work?
2
Answers
Thanks to everyone for participating. After some research on what @hatana mentioned, i found a solution - the event bus. It does exactly what I wanted.
If you expected
globalThis.setPanelActive
to work assetPanelActive
with the assignment you are doing, then you are wrong, hooks are designed to work within the scope of a component in this case you have to use a context on a higher lever something like this:now when you use
setPanelActive
from any componentuseEffect
should fire