Skip to content
作者:  WHY
字数统计: 
阅读时长:  分钟
阅读量: 

React Navigation

hook

useNavigation

useRoute

Screen 组件入参

route

tsx
export default function ProfileScreen({route}) {
  return (
    <View>
      <Text>This is the profile screen of the app</Text>
      <Text>{route.key}</Text>
      <Text>{route.name}</Text>
      <Text>{route.path}</Text>
      <Text>{route.params}</Text>
    </View>
  );
}
tsx
export default function ProfileScreen({navigation}) {
  return (
    <View>
      <Text onPress={() => navigation.navigate('main')}>To main screen</Text>
    </View>
  );
}

TIP

  • screen 组件可解构出 navigation, screen 组件的子组件内无法解构出 navigation

  • 可使用 useNavigation hook 或使用 NavigationContext:

    tsx
    import {NavigationContext} from '@react-navigation/native';
    
    function SomeComponent() {
      const navigation1 = useNavigation();
      // We can access navigation object via context
      const navigation2 = React.useContext(NavigationContext);
      console.log(navigation1 === navigation2);
    }
  • 或使用 ref 获取全局 navigation

Contributors

The avatar of contributor named as why why

Changelog

Released under the MIT License.