您的当前位置:首页正文

【翻译】ReactNavigation/Configuring

来源:华拓网

只有StackNavigator导航系统支持设置头部。

在之前的例子中,我们在App中创建了一个StackNavigation来展示几个界面。
当导航到一个会话界面的时候,我们可以通过向navigate方法中传递参数来向新的路由传递。在这个时候,我们希望在会话界面中提供人的名字(Lucy)。

this.props.navigation.navigate('Chat', { user:  'Lucy' });

user参数可以被会话界面所接收:

class ChatScreen extends  {
  render() {
    const { params } = this.props.navigation.state;
    return <Text>Chat with {params.user}</Text>;
  }
}

Setting the Header Title - 设置头部标题

Next, the header title can be configured to use the screen param:
下面,可以使用参数来定义头部标题:

class ChatScreen extends  {
  static navigationOptions = ({ navigation }) => ({
    title: `Chat with ${navigation.state.params.user}`,
  });
  ...
}

Adding a Right Button - 添加一个右侧按钮

static navigationOptions = {
  headerRight: <Button title="Info" />,
  ...
static navigationOptions = ({ navigation }) => {
  const {state, setParams} = navigation;
  const isInfo = state.params.mode === 'info';
  const {user} = state.params;
  return {
    title: isInfo ? `${user}'s Contact Info` : `Chat with ${state.params.user}`,
    headerRight: (
      <Button
        title={isInfo ? 'Done' : `${user}'s info`}
        onPress={() => setParams({ mode: isInfo ? 'none' : 'info'})}
      />
    ),
  };
};