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

useNavigationComponentDidAppear not working when mixed with react-native-safe-area-context's SafeAreaProvider

Open benpeke opened this issue 5 years ago • 2 comments

Working with both RNN hooks and SafeAreaProvider breaks the useNavigationComponentDidAppear hook.

Navigation.registerComponent(
      componentName,
      () => (props: any): React.ReactElement<any> => (
        <SafeAreaProvider>
            <NavigationProvider value={{ componentId: props.componentId }}>
                <Component {...props} />
            </NavigationProvider>
        <SafeAreaProvider>
      ),
      () => Component
);

And in the Component useNavigationComponentDidAppear is never called. Verified by adding log statements to Navigation.events().registerComponentDidAppearListener. In the logs I can only see the output from registerComponentDidAppearListener however useNavigationComponentDidAppear never shows any logs for certain components.

benpeke avatar Jan 28 '21 22:01 benpeke

Hi @benpeke thank you for reporting this but unfortunately I couldn't reproduce the issue and it's actually working:

image

This is how I'm registering the component as you mention

Navigation.registerComponent(
  'WelcomeScreen',
  () => props => (
    <SafeAreaProvider>
      <NavigationProvider value={{ componentId: props.componentId }}>
        <WelcomeScreen {...props} />
      </NavigationProvider>
    </SafeAreaProvider>
  ),
  () => WelcomeScreen
)

and that's how I'm using it inside the actual screen:

import React from 'react'
import { Text } from 'react-native'
import { NavigationFunctionComponent } from 'react-native-navigation'
import { useNavigationComponentDidAppear } from 'react-native-navigation-hooks'

const WelcomeScreen: NavigationFunctionComponent = () => {
  useNavigationComponentDidAppear(() => {
    console.warn('component did appeared')
  })

  return <Text>WelcomeScreen</Text>
}

export default WelcomeScreen

I'm going to close this issue for now. If you are still facing it please try with a project from scratch and if it doesn't not work publish it as a public repo so we can troubleshoot it.

jpgarcia avatar Jan 30 '21 21:01 jpgarcia

First of thank you for your effort and great work @jpgarcia This issue is definitely valid, weird think is with console log expirements:

1- useComponentDidAppear hooks inside Navigation.events().registerComponentDidAppearListener not firing 2- outside of hooks random Navigation.events().registerComponentDidAppearListener always fires 3- componentId in useComponentDidAppear is valid same with my component

Basic information:

Provider Wrapper

export const ComponentSetupWrapper: (
  Component: NavigationFunctionComponent,
) => (props: any) => ReactElement = Component => {
  return props => (
    <SafeAreaProvider>
      <NavigationProvider value={{componentId: props.componentId}}>
        <AuthorizationProvider>
          <Component {...props} />
        </AuthorizationProvider>
      </NavigationProvider>
    </SafeAreaProvider>
  );
};

Landing Page Listener

export const LandingPage: NavigationFunctionComponent = ({componentId}) => {
  useNavigationComponentDidAppear(() => {
    initializeDevice()
      .then(hasSession => {
        if (hasSession) {
          Navigation.setRoot(AppStackRoot);
        } else {
          Navigation.setRoot(AuthStackRoot);
        }
      })
      .catch(console.error);
  }, componentId);

  const {user, accessToken, device} = useAuthorizationState();
  const {initializeDevice} = useAuthorizationActions();

  useEffect(() => {}, [user, device, accessToken]);

Versions: "react-native-navigation": "^7.28.0", "react-native-navigation-hooks": "^6.3.0", "react-native-safe-area-context": "^4.3.1"

For now, I'll keep writing my own hook

Edit

I realize if any of the components are surrounded with SafeAreaProvider, somehow re-renders in very atomic moment that componentDidAppear fired which totally makes sense I guess, because it also consumes iOS View controllers or Android Fragments on....(I can't remember the name) in order to consume layout changes with appearance

This issue should move in to RNN or SafeAreaContext

yadigarbz avatar Jun 18 '22 21:06 yadigarbz