
맨 처음 react-navigaton 설치 후, 타입 지정하지 않으면 아래 같은 에러가 난다.
Type '({ navigation }: MainScreenProps) => Element' is not assignable to type 'ScreenComponentType<ParamListBase, "Main"> | undefined'.
Type '({ navigation }: MainScreenProps) => Element' is not assignable to type 'FunctionComponent<{}>'.
Types of parameters '__0' and 'props' are incompatible.
Type '{}' is missing the following properties from type 'MainScreenProps': navigation, route
//타입이 없다고 한다!
이를 해결하기 위해, 타입을 미리 지정해 줘야하는데,
import {NativeStackScreenProps} from '@react-navigation/native-stack';
// route를 통해 전달받을 인자가 없다?! 그럼 undefined, 혹은 route에서 받을 인자의 타입을 쓰는 것임
export type RootStackParamList = {
Main: undefined;
Plogging: {
shouldOpenModal?: boolean;
};
Camera: undefined;
};
// 여기는 MainPage를 위한 Props로 위에 있는 RootStackParamList 중에서
// 해당하는 페이지인 'Main'를 지정해주어 메인 화면에 특화된 속성을 전달함.
export type MainScreenProps = NativeStackScreenProps<
RootStackParamList,
'Main'
>;
// 새로운 페이지를 정의해보고, props를 지정하자.
// 먼저 navigation과 route를 props로 받는데!
export default function PloggingPage({navigation, route}: PloggingScreenProps) {
...
//route는 특정 페이지에서 이동해왔을 때, 값을 받아오는데 사용!
useEffect(() => {
if (route.params?.shouldOpenModal === true) {
setModalVisible(true);
}
}, [route.params]);
...
// 아래와 같이 이동할 때 props에 페이지 이름을 추가해서 이동한다.
<Button
title="Go to Plogging"
onPress={() => navigation.navigate('Camera')}
/>