我正在try 注入自定义头,以响应使用钩子的功能组件.

例如,我有以下带有一些钩子的React功能组件:

function StoryHome(props) {
  const [communityObj, setCommunityObj] = useState({
    uid: null,
    name: null,
  });
...
}

StoryHome.navigationOptions = {
  title: 'Stories',
  headerTitleStyle: {
    textAlign: 'left',
    fontFamily: 'OpenSans-Regular',
    fontSize: 24,
  },
  headerTintColor: 'rgba(255,255,255,0.8)',
  headerBackground: (
    <LinearGradient
      colors={['#4cbdd7', '#3378C3']}
      start={{ x: 0, y: 1 }}
      end={{ x: 1, y: 1 }}
      style={{ flex: 1 }}
    />
  ),
  headerRightContainerStyle: {  
    paddingRight: 10,   
  },    
  headerRight: (    
    <TouchableOpacity onPress={navigation.navigate('storiesList')}> 
      <Ionicons 
         name="ios-search-outline"  
         size={25}  
         color="white"  
         left={20}  
       />   
    </TouchableOpacity>
  )
};

export default StoryHome;

所以这类工作,除了TouchableOpacity部分.

首先,我无法正确渲染Ionicon,其次,我无法访问功能组件之外的navigation对象.

我很想继续使用钩子,但似乎无法解决这个问题.

推荐答案

navigationOptions可以是一个函数,它以对象作为参数,以navigation作为属性.

您还需要确保为onPress指定一个函数,并且不要直接调用navigation.navigate.

StoryHome.navigationOptions = ({ navigation }) => ({
  title: "Stories",
  headerTitleStyle: {
    textAlign: "left",
    fontFamily: "OpenSans-Regular",
    fontSize: 24
  },
  headerTintColor: "rgba(255,255,255,0.8)",
  headerBackground: (
    <LinearGradient
      colors={["#4cbdd7", "#3378C3"]}
      start={{ x: 0, y: 1 }}
      end={{ x: 1, y: 1 }}
      style={{ flex: 1 }}
    />
  ),
  headerRightContainerStyle: {
    paddingRight: 10
  },
  headerRight: (
    <TouchableOpacity onPress={() => navigation.navigate("storiesList")}>
      <Ionicons name="ios-search" size={25} color="white" left={20} />
    </TouchableOpacity>
  )
});

React-native相关问答推荐

如何在API调用期间将本地存储(Expressc存储)中的令牌传递到后台

领带麦克风在Reaction-Native-Incall-Manager中不工作

@react-native-community/cli-platform-ios/native_modules' 来自哪里?

React-Native-Web 错误:rnw_blogpost.bundle.js:1414 Uncaught TypeError: Cannot read properties of undefined (reading 'isBatchingLegacy')

为什么将字符串写入文件然后将相同的文件作为字符串读取会导致不同的字符串?

React-native 重置数据库

在向后滑动时执行操作(react-navigation )

找不到变量 atob

你如何在生产模式下运行 react-native 应用程序?

React Native Card Carousel 视图?

在 React Native 中使用多个上下文提供程序的更好方法

React-Native Android 中隐藏(hide)的元素溢出

Xcode 设备不可用,未找到运行时配置文件

React native Redux - 对象不是构造函数(判断'new ctor(props context)')

自定义原生基础的选项卡

如何使用 Stripe (stripe.js) 和 react-native

React Native Bullet Character? or Unicode?

在 FlatList 上使用 React Native 搜索过滤器

如何为 TouchableOpacity 组件创建禁用样式?

react-native构建错误:package android.support.annotation does not exist