35

I am currently using react-navigation to do stack- and tab- navigation.

Is it possible to re-render a component every time the user navigates to specific screens? I want to make sure to rerun the componentDidMount() every time a specific screen is reached, so I get the latest data from the server by calling the appropriate action creator.

What strategies should I be looking at? I am pretty sure this is a common design pattern but I failed to see documented examples.

1
  • 1
    Are you using mode : modals or classic navigation ? Commented Oct 14, 2018 at 19:27

6 Answers 6

59

If you are using React Navigation 5.X, just do the following:

import { useIsFocused } from '@react-navigation/native'

export default function App(){

    const isFocused = useIsFocused()

    useEffect(() => {
        if(isFocused){
            //Update the state you want to be updated
        }
    }, [isFocused])

}

The useIsFocused hook checks whether a screen is currently focused or not. It returns a boolean value that is true when the screen is focused and false when it is not.

7
  • 1
    Just saying, thanks for sharing isFocused but this does not show a way to rerender anything and thus, can't be the accepted answer. I can't find a way to rerender the view with it. I had to ask the question once more here Commented Sep 8, 2020 at 12:14
  • Well, I think there was a mistake on your part regarding the functioning of the code I talked about. This code is for you to know and update any state when the screen is FOCUSED, this code does not force a rendering. Commented Sep 8, 2020 at 12:35
  • The question was about re-render (first word of the question), I finally ended up using key on a component` and setMyKey(Math.random()) Commented Sep 8, 2020 at 12:38
  • 2
    I still think you should show how to re-render as this is the top choice when googling the question. Commented Sep 8, 2020 at 17:30
  • 1
    There is an issue with this code, this is called both when screen focussed or blurrs, which may not be required if the code is resource intensive (like an api call) Commented Oct 23, 2020 at 21:34
5

React Navigation lifecycle events quoted from react-navigation

React Navigation emits events to screen components that subscribe to them. There are four different events that you can subscribe to: willFocus, willBlur, didFocus and didBlur. Read more about them in the API reference.


Let's check this out,

With navigation listeners you can add an eventlistener to you page and call a function each time your page will be focused.

const didBlurSubscription = this.props.navigation.addListener(
  'willBlur',
  payload => {
    console.debug('didBlur', payload);
  }
);

// Remove the listener when you are done
didBlurSubscription.remove();

Replace the payload function and change it with your "refresh" function.

Hope this will help.

0
4

You can also use also useFocusEffect hook, then it will re render every time you navigate to the screen where you use that hook.

useFocusEffect(()=> {
    your code
})
1
1

At the request of Dimitri in his comment, I will show you how you can force a re-rendering of the component, because the post leaves us with this ambiguity.

If you are looking for how to force a re-rendering on your component, just update some state (any of them), this will force a re-rendering on the component. I advise you to create a controller state, that is, when you want to force the rendering, just update that state with a random value different from the previous one.

0

This one works fine: We listen to the focus event which trigger whenever the current screen is printed. In which case, we call the callback function attached that define what we want to do. And don't forget to remove the event listener when you're done.

import { useNavigation } from "@react-navigation/native";
const navigation = useNavigation();
useEffect(() => {
  const didFocus = navigation.addListener(
    'focus',
    () => {
      yourFunctionToCall();
    }
  );
  
  // Remove the listener when you are done
  return didFocus;
}, [navigation]
);
1
  • 1
    Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. Commented Oct 19, 2023 at 11:02
-2

To trigger a render when navigating to a screen.

import { useCallback } from "react"; 
import { useFocusEffect } from "@react-navigation/native";  

// Quick little re-render hook       
function useForceRender() {
    const [value, setValue] = useState(0);
    return [() => setValue(value + 1)];
}

export default function Screen3({ navigation }) {
  const [forceRender] = useForceRender();

  // Trigger re-render hook when screen is focused
  // ref: https://reactnavigation.org/docs/use-focus-effect
  useFocusEffect(useCallback(() => { 
    console.log("NAVIGATED TO SCREEN3")
    forceRender();
  }, []));
}

Note:

"@react-navigation/native": "6.0.13",
"@react-navigation/native-stack": "6.9.0",

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.