react-native-navigation-hooks icon indicating copy to clipboard operation
react-native-navigation-hooks copied to clipboard

Not possible to call useNavigationComponentDidAppear twice

Open IsaacIsrael opened this issue 3 years ago • 2 comments

When I try to call twice the second one is ignored like this

useNavigationComponentDidAppear(() => {
    console.log('test');
    setIsScreen(true);
  });

  useNavigationComponentDidAppear(() => {
    console.log('test 2');
  });

IsaacIsrael avatar Mar 01 '22 23:03 IsaacIsrael

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.

hancandice avatar Jun 14 '22 16:06 hancandice

Thank for the explanation , Yep that should change that , as you ask them :)

IsaacIsrael avatar Jun 26 '22 07:06 IsaacIsrael