화면이 포커스 되었을때 실행해야 되는 함수를
이런식으로 실행하는 사람들이 분명있을 것이다.
react native에는 useFocusEffect라는 훅이 있다. 공식문서에서도 이것을 사용하는것을 권장한다.
https://reactnavigation.org/docs/use-focus-effect/
useFocusEffect | React Navigation
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. While this can be achieved using focus and blur events, it's not very ergonomic.
reactnavigation.org
useEffect를 사용한다면 클린업 작업이라든지 최적화 작업을위해 추가적인 코드를 작성해야한다. 하지만 useFocsEffect의 경우
클린업 함수를 자동적으로 처리를 해주는 장점이있다. 사용법 또한 그리 어렵지 않으니 react-native프로젝트를 진행하고있다면 useFocusEffect를 사용하는 것을 적극 권장한다.
react native vs react-native-gesture-handler (scrollview 비교) (0) | 2025.02.06 |
---|---|
react navigation v7 (0) | 2024.11.26 |
개발중인앱(5) - 라이브업데이트편 (0) | 2024.08.30 |
개발중인앱(4)-react-native-vision-camera (0) | 2024.08.30 |
개발중인앱(3) - 애니메이션으로 인해 길어지는 코드 관리 방법 (0) | 2024.08.30 |
댓글 영역