react-navigation随记

2017-07-13  本文已影响263人  埃米莉Emily
  1. 我们可以在navigationOptions中配置屏幕选项,比如title
class HomeScreen extends React.Component {
    static navigationOptions = {
      title: 'Welcome',
    };
    render() {
      return <Text>Hello, Navigation!</Text>;
    }
}
  1. 使用screen navigation prop中的navigate可跳转到指定屏幕上。同时我们可以早在其中放入新路由页面需要的数据,如下例中的user
class HomeScreen extends React.Component {
    render() {
      const { navigate } = this.props.navigation;
      return (
        <View>
          <Text>Hello, Chat App!</Text>
          <Button
            onPress={() => navigate('Chat', { user: 'Lucy' })}
            title="Chat with Lucy"
          />
        </View>
      );
    }
}
// 当组件加入navigator,就会带有navigation这个props属性
const SimpleApp = StackNavigator({
    Home: { screen: HomeScreen },
    Chat: { screen: ChatScreen },
});
// this.props.navigation.state
class ChatScreen extends React.Component {
    static navigationOptions = ({ navigation }) => ({
      title: `Chat with ${navigation.state.params.user}`,
    });
    render() {
      return (
        <View>
          <Text>Chat with {this.props.navigation.state.params.user}</Text>
        </View>
      );
    }
}
  1. navigation prop
{
       // the name of the route config in the router
       routeName: 'profile',
       //a unique identifier used to sort routes
       key: 'main0',
       //an optional object of string options for this screen
       params: { hello: 'world' }
}
render() {
      const { goBack } = this.props
      return (
        <View>
          <Button
            onPress={() => goBack()}
            title="Go back from this HomeScreen"
          />
          <Button
            onPress={() => goBack(null)}
            title="Go back anywhere"
          />
          <Button
            onPress={() => goBack('screen-123')}
            title="Go back from screen-123"
          />
        </View>
      )
}
  1. Navigation Options 配置屏幕导航选项
    我们有两种方式,一是静态配置,如本文第一点;二是动态配置,来自props,包含:
class ProfileScreen extends React.Component {
      static navigationOptions = ({ navigation, screenProps }) => ({
        title: navigation.state.params.name + "'s Profile!",
        headerRight: <Button color={screenProps.tintColor} {...} />,
      });
  ...
<SimpleApp
      screenProps={{tintColor: 'blue'}}
      // navigation={{state, dispatch}} // optionally control the app
/>
const MyTabNavigator = TabNavigator({
      profile: ProfileScreen,
      ...
 }, {
      navigationOptions: {
      headerTintColor: 'blue',
  },
});
  1. Navigation Actions 提供了以下action:
import { NavigationActions } from 'react-navigation'
const navigateAction = NavigationActions.navigate({
        routeName: 'Profile',
        params: {},
        action: NavigationActions.navigate({ routeName: 'SubProfileRoute'})
})
this.props.navigation.dispatch(navigateAction)
import { NavigationActions } from 'react-navigation'
const resetAction = NavigationActions.reset({
      index: 1,  // 用于指定当前活动路由,此处是 ‘Settings‘
      actions: [    // 导航动作的阵列将取代导航状态
        NavigationActions.navigate({ routeName: 'Profile'}),
        NavigationActions.navigate({ routeName: 'Settings'})
      ]
})
this.props.navigation.dispatch(resetAction)
import { NavigationActions } from 'react-navigation'
const backAction = NavigationActions.back({
        key: 'Profile'   // 如果设置,导航将从给定的键返回。如果为空,导航将返回任何地方
})
this.props.navigation.dispatch(backAction)
import { NavigationActions } from 'react-navigation'
const setParamsAction = NavigationActions.setParams({
      params: { title: 'Hello' },
      key: 'screen-123',
})
this.props.navigation.dispatch(setParamsAction)
上一篇 下一篇

猜你喜欢

热点阅读