react native - navigation focus

바람찬허파·2023년 1월 16일
0

react-native

목록 보기
1/1

로그아웃 기능을 구현 하던 중,
로그아웃 하시겠습니까? 창에서 문제가 생겼다.


해당 창에서 아니오를 누르면 -> 이전 화면으로 이동한다.
하지만 다시 로그아웃을 버튼을 누르면 alert 창이 뜨지 않는다.

화면이 다시금 focus 되었을 때 다시 Alert 함수가 실행되어야 함을 알게 되었다.

Hook to run an effect in a focused screen, similar to React.useEffect. This can be used to perform side-effects such as fetching data or subscribing to events. The passed callback should be wrapped in React.useCallback to avoid running the effect too often.

export const Logout = ({ navigation: { goBack } }) => {
  useFocusEffect(
    //화면으로 들어왔을 때
    React.useCallback(() => {
      Alert.alert(
        "로그아웃 하시겠습니까?",
        "",
        [
          {
            text: "네",
            onPress: () => {
              firebase.auth().signOut();
            },
          },
          {
            text: "아니요",
            onPress: () => {
              return (goBack());
            },
          },
        ],
        { cancelable: false }
      );
      console.log("들어옴");
    }, [])
  );
};

참고 https://nickname33.tistory.com/m/25

0개의 댓글