react-native-navigation-hooks
react-native-navigation-hooks copied to clipboard
Not possible to call useNavigationComponentDidAppear twice
When I try to call twice the second one is ignored like this
useNavigationComponentDidAppear(() => {
console.log('test');
setIsScreen(true);
});
useNavigationComponentDidAppear(() => {
console.log('test 2');
});
const useNavigationComponentDidAppear = (
handler: EventHandler,
componentId: string
) => {
useLayoutEffect(() => {
const subscription = Navigation.events().registerComponentDidAppearListener(
(event: ComponentDidAppearEvent) =>
triggerIfComponentIdMatches(handler, event, componentId)
);
return () => subscription.remove();
}, [handler, componentId]);
};
As you can see @IsaacIsrael, useNavigationComponentDidAppear works by using useLayoutEffect to registerComponentDidAppearListener to navigation events.
So if you use setState inside useNavigationComponentDidAppear , the next call will not be called because the component will be updated before the next effect is executed.
Thank for the explanation , Yep that should change that , as you ask them :)