Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use useFocusEffect hook

As the docs https://reactnavigation.org/docs/en/next/use-focus-effect.html,

"Sometimes we want to run side-effects when a screen is focused. A side effect may involve things like adding an event listener, fetching data, updating document title, etc."

I'm trying to use useFocusEffect to fetch data everytime that the user go to that page.

on my component I have a function which dispatch an action with redux to fetch the data:

const fetchData = ()=>{ dispatch(companyJobsFetch(userDetails.companyId)); };

Actually I'm using useEffect hook to call fetchData(), but I'd like to fetch data everytime that the user go to that page and not only when rendered the first time.

It's not clear from the documentation how to use useFocusEffect and I'm not having success on how to do it.

Any help?

like image 881
Francesco Clementi Avatar asked Nov 17 '19 18:11

Francesco Clementi


People also ask

How do you use useFocusEffect?

x. useFocusEffect: Use to trigger any function or method with screen gets focused or unfocused. useIsFocused: Use to refresh UI components from the return method when the screen gets focused or unfocused.

What is the difference between useEffect and useFocusEffect?

The useFocusEffect is analogous to React's useEffect hook. The only difference is that it only runs if the screen is currently focused. The effect will run whenever the dependencies passed to React.

When should I use Layouteffect?

useLayoutEffect. The signature is identical to useEffect , but it fires synchronously after all DOM mutations. Use this to read layout from the DOM and synchronously re-render. Updates scheduled inside useLayoutEffect will be flushed synchronously, before the browser has a chance to paint.

How do I use the useeffect hook?

The useEffect Hook allows you to perform side effects in your components. Some examples of side effects are: fetching data, directly updating the DOM, and timers. useEffect accepts two arguments. The second argument is optional. Let's use a timer as an example. Use setTimeout () to count 1 second after initial render: But wait!!

How does the usefocuseffect work?

The useFocusEffect is analogous to React's useEffect hook. The only difference is that it only runs if the screen is currently focused. The effect will run whenever the dependencies passed to React.useCallback change, i.e. it'll run on initial render (if the screen is focused) as well as on subsequent renders if the dependencies have changed.

What does the useisfocused hook Do?

The useIsFocused hook will cause our component to re-render when we focus and unfocus a screen. Using this hook component may introduce unnecessary component re-renders as a screen comes in and out of focus.

What is react’s useeffect hook?

If you’re familiar with React class lifecycle methods, you can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined. There are two common kinds of side effects in React components: those that don’t require cleanup, and those that do. Let’s look at this distinction in more detail.


2 Answers

The docs show you how to do it. You need to replace API.subscribe with your own thing:

useFocusEffect(
  React.useCallback(() => {
    dispatch(companyJobsFetch(userDetails.companyId));
  }, [dispatch, companyJobsFetch, userDetails.companyId])
);
like image 156
satya164 Avatar answered Sep 17 '22 10:09

satya164


For version react navigation 4.x, you can use addEvent listener

useEffect(() => {
    if (navigation.isFocused()) {
      resetReviews(); // replace with your function
    }
  }, [navigation.isFocused()]);

OR

useEffect(() => {
    const focusListener = navigation.addListener('didFocus', () => {
      // The screen is focused
      // Call any action
      _getBusiness({id: business?.id}); // replace with your function
    });
    return () => {
      // clean up event listener
      focusListener.remove();
    };
  }, []);

For later version 5.x, you can use hooks to achieve this

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

// ...

function Profile() {
  const isFocused = useIsFocused();

  return <Text>{isFocused ? 'focused' : 'unfocused'}</Text>;
}
like image 44
p8ul Avatar answered Sep 18 '22 10:09

p8ul