0

I am a beginner to react and react-native. I am trying to set up Firebase for analytics. I am using react-native-redux with react-native-redux-persist. Also using react-naviagation.

The code from App.js in my react native app:

const App = () => {
  const routeNameRef = React.useRef(null);
  const navigationRef = React.useRef(null);

  React.useEffect(() => {
    console.log('hrhr');
    console.log(routeNameRef);
    console.log(navigationRef);
    const state = navigationRef.current.getRootState();

    // Save the initial route name
    routeNameRef.current = getActiveRouteName(state);
  }, []);

  return (
    <Provider store={persistStore.store}>
      <PersistGate loading={null} persistor={persistStore.persistor}>
        <NavigationContainer
          ref={navigationRef}
          onStateChange={state => {
            const previousRouteName = routeNameRef.current;
            const currentRouteName = getActiveRouteName(state);

            if (previousRouteName !== currentRouteName) {
              // The line below uses the @react-native-firebase/analytics tracker
              // Change this line to use another Mobile analytics SDK
              analytics().setCurrentScreen(currentRouteName, currentRouteName);
            }

            // Save the current route name for later comparision
            routeNameRef.current = currentRouteName;
          }}>
           {/***/}
        </NavigationContainer>
      </PersistGate>
    </Provider>
  );
};

Logs:

LOG  hrhr
LOG  {"current": undefined}
LOG  {"current": undefined}

Versions:

"react": "16.13.1",
"react-native": "0.61.5",

The app also has dependencies on react native redux and firebase.

UPDATE

useEffect is sometimes being called on loop after the app reloads, even though I am passing an empty array in useEffect().

UPDATE

After removing PersistGate JSX tags, it works fine. Please let me know how can I fix this.

11
  • 1
    what's wrong with that? you must initialize the current property before use it. Commented Apr 19, 2020 at 3:02
  • Shouldn't react initialise the current value? Commented Apr 19, 2020 at 3:43
  • The useRef hook still needs to be initialized to something before the first use. It isn't possible for react to know what you want that initial value to be. Can you disclose more of the component code, especially where navigationRef.current is set to be able to invoke getRootState? Commented Apr 19, 2020 at 4:06
  • reactnavigation.org/docs/screen-tracking I am trying to follow this documentation. Commented Apr 19, 2020 at 4:10
  • I have updated the code. Commented Apr 19, 2020 at 4:19

2 Answers 2

1

This is working when I extract the complete <NavigationContainer> in a separate component and use useEffect there. Don't know what was the root cause still.

Sign up to request clarification or add additional context in comments.

Comments

1

It's only undefined on load. What you want is to use a useCallback since you are setting the ref after load. check out https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.