繁体   English   中英

react-navigation 中的动态 header

[英]Dynamic header in react-navigation

我正在使用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会使用有效负载 object(“配置文件”)进行初始化:

Search.js / Visitors.js

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

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

因此,如何在Profile组件中创建 header 按钮,以便我可以访问配置文件 object?

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

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

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

<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 文档

实际上,我发现可以解决这个问题 - 它可以通过在Profile.js中添加 header 来解决(其中 object 可用)而不是在App.js中。

所以我刚刚删除了headerRightApp.js的代码,而是把它放在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 ' state 中的当前配置文件 object。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM