【问题标题】:Dynamic header in react-navigation反应导航中的动态标题
【发布时间】:2020-04-09 11:41:51
【问题描述】:

我正在使用react-navigation v5.0.0 的函数式方法,并且我有一个堆栈导航器,其中包含:

App.js

<Stack.Screen
   name="Profil"
   component={Profile}
   options={{
      headerStyle: {
         backgroundColor: '#2270b9'
      },
      headerTintColor: '#fff',
      headerTitleStyle: {
         color: 'white'
      },
      headerRight: () => (
         <View style={{ flex: 1, flexDirection: 'row' }}>
            <Ionicons
               style={{ color: 'white', marginRight: 15, marginTop: 5 }}
               size={32}
               onPress={() => { _sendMessage(...) }}            // <--- problem is here
               name="ios-mail"
               backgroundColor="#CCC"
            />
         </View>
      )
   }}
/>

Profile 组件大致如下:

Profile.js

export default function Profile({ route, navigation }) {
   const { profile } = route.params;

   return (
      <SafeAreaView style={styles.container}>
         <View style={styles.container}>
            <ScrollView contentContainerStyle={styles.contentContainer}>
   [...]

现在的问题是,Profile 在打开配置文件时使用有效负载对象(“配置文件”)进行初始化:

Search.js / Visitors.js

navigation.navigate('Profil', { profile });

问题是,App.js 中添加的发送按钮需要作为路由参数传递给Profile.js 的配置文件对象,但在App.js 中不可用。

如何在Profile 组件中创建标题按钮,以便访问配置文件对象?

【问题讨论】:

    标签: javascript react-native header navigation react-navigation


    【解决方案1】:

    您可以尝试修改您的选项道具以将路线作为参数,如下所示:

    options={({ route: { params: { profile } } }) => ({
      /** use profile here */
    })
    

    要将它放在您的 App.js 的上下文中,请注意 options 是如何将 { route } 作为参数并返回您的选项对象的函数。

    <Stack.Screen
       name="Profil"
       component={Profile}
       options={({ route: { params: { profile } } }) => ({ // <- Note that options in now a function
          headerStyle: {
             backgroundColor: '#2270b9'
          },
          headerTintColor: '#fff',
          headerTitleStyle: {
             color: 'white'
          },
          headerRight: () => (
             <View style={{ flex: 1, flexDirection: 'row' }}>
                <Ionicons
                   style={{ color: 'white', marginRight: 15, marginTop: 5 }}
                   size={32}
                   onPress={() => { _sendMessage(profile) }} // <--- you can use profile here
                   name="ios-mail"
                   backgroundColor="#CCC"
                />
             </View>
          )
       })}
    />
    

    react-navigation docs

    【讨论】:

    • 不幸的是,App.js 中不知道配置文件对象,我在其中构建屏幕。
    • 你试过了吗?它不需要在 App.js 中知道,React Navigation 允许根据导航到屏幕时收到的参数构建屏幕选项。
    • 好的,您建议将其添加到component={Profile} 中的App.js?右边header的onPress回调中需要profile对象,怎么用上面的sn-p访问呢?
    • 我已经使用您的代码 sn-p 作为上下文更新了答案。 options prop 可以是一个以 { route, navigation } 作为参数的函数,让您可以从 route.params 访问配置文件对象。
    • 啊,我明白了。感谢您的澄清。
    【解决方案2】:

    实际上,我发现可以解决这个问题 - 可以通过在 Profile.js(对象可用的地方)而不是 App.js 中添加标题来解决。

    所以我刚刚删除了App.jsheaderRight 的代码,而是将其放入Profile.js

    export default function Profile({ route, navigation }) {
       const { profile } = route.params;
    
       React.useLayoutEffect(() => {
          navigation.setOptions({
             headerRight: () => (
                <View style={{ flex: 1, flexDirection: 'row' }}>
                   <Ionicons
                      style={{ color: 'white', marginRight: 15, marginTop: 5 }}
                      size={32}
                      onPress={_onSendMessage}
                      name="ios-mail"
                      backgroundColor="#CCC"
                      enabled={ profile && profile.core ? true : false}
                   />
                </View>
             )
          });
        }, []);
    

    这样,无论从哪里打开Profile.js,按钮回调都可以访问当前处于Profile.js'状态的配置文件对象。

    【讨论】:

    • 这是一个有效的解决方案。然而,一个小缺点是按钮只会在第一次渲染后出现,这可能不是您想要的。
    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2020-01-07
    • 1970-01-01
    相关资源
    最近更新 更多