我有以下两段代码:

CustomHeader.tsx

import { View, StyleSheet, Button } from 'react-native';
import { NavigationScreenProps } from 'react-navigation';
import Icon from 'react-native-vector-icons/Ionicons';


export  const CustomHeader = ({ navigation }: NavigationScreenProps) => (
    <View style={[styles.container]}>
      <Icon
        name="md-menu"
        size={32}
        color="black"
        style={{ marginLeft: 10 }}
        onPress={() => navigation.openDrawer()}
      />
    </View>
  );

  const styles = StyleSheet.create({
    container: {
      borderBottomWidth: 2,
      height: 70,
      paddingTop: 20,
    },
  });

DetailScreen.tsx

import React from 'react';
import { Text, View, Button, Alert } from 'react-native';
import { NavigationScreenProps } from "react-navigation";
import { CustomHeader } from '../components/Header';

export class ChangeAccountDetailScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <CustomHeader navigation={this.props.navigation} />
        <Text style={{ fontSize: 20 }}>Profile Screen</Text>
      </View>
    );
  }
}

在detailscreen中,我得到以下错误:

Property 'navigation' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'.

我搜索了这个问题,我知道它有一个问题,即我没有在CustomHeader中声明类型.然而,我不知道如何解决这个问题.我对打字有点陌生.有人能给我解释一下如何解决这类问题吗?

推荐答案

我可能错了,但你试过增加navigation种预期的类型吗

import React from 'react';
import { Text, View, Button, Alert } from 'react-native';
import { NavigationScreenProps } from "react-navigation";
import { CustomHeader } from '../components/Header';

interface Props {
  navigation: any
}

export class ChangeAccountDetailScreen extends React.Component<Props> {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <CustomHeader navigation={this.props.navigation} />
        <Text style={{ fontSize: 20 }}>Profile Screen</Text>
      </View>
    );
  }
}

React-native相关问答推荐

React native React native下拉 Select 器下拉列表不透明问题

一旦我关闭应用程序,Json struct 就会发生变化

何时在 React Native 中使用辅助功能 Role='link'?

react-native react-native-vector-icons:如何使用字体真棒图标

如何在 React Native 的嵌套 Touchable 中传播touch 事件?

React Native WebView 中的 Javascript console.log()

如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL

React Native - 初始属性 Android

如何在 react-native 中在子级和父级之间传递数据?

xcode 8.3 框架未找到架构 armv7 的 FileProvider

使用 React Native 打开 iOS iPad 模拟器

React-Native:FlatList onRefresh not called on pull up.

React Native 创建自定义组件/库

react-native如何禁用android开发模式

在 webview 中检测touch 是 Apple Pencil 还是手指(react-native)

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

在 React Native 中检测向左滑动

为什么我收到警告:函数作为 React 子级无效?

如何在 React Native 中创建拖放操作?

React Native 的最佳文件 Select 器