【问题标题】:how to solve Property 'navigation' does not exist on type 'Readonly<{}> &如何解决“Readonly<{}> 类型上不存在属性“导航”&
【发布时间】:2019-09-08 07:21:30
【问题描述】:

我有以下两段代码:

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中我收到以下错误:

类型'Readonly & Readonly'。

我搜索了这个问题,我知道它有一些事实,即我没有在我的 CustomHeader 中声明类型。但是我不知道如何解决这个问题。我对打字稿有点陌生。有人可以向我解释如何解决这种类型的问题吗?

【问题讨论】:

  • 哪一行导致该错误?顺便说一句,type Props = NavigationScreenProps; Props 类型,你的意思是extends React.Component&lt;Props&gt;
  • 您好,对不起,我忘了删除那行。我更新了代码..我得到错误的行是&lt;CustomHeader navigation={this.props.navigation} /&gt;,特别是this.pros.navigation
  • 您尝试过我建议的修复方法吗?
  • 嘿,您建议的修复方法解决了它!非常感谢。

标签: typescript react-native react-navigation


【解决方案1】:
import React from 'react';
import { Button } from 'react-native';
import { withNavigation } from 'react-navigation';

class MyBackButton extends React.Component {
  render() {
    return <Button title="Back" onPress={() => { this.props.navigation.goBack() }} />;
  }
}

// withNavigation returns a component that wraps MyBackButton and passes in the
// navigation prop
export default withNavigation(MyBackButton);

https://reactnavigation.org/docs/en/connecting-navigation-prop.html

【讨论】:

    【解决方案2】:

    我可能是错的,但是您是否尝试过添加 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>
        );
      }
    }
    

    【讨论】:

    【解决方案3】:

    这是另一个解决方案,我在接口定义中添加了更多细节

    import React from 'react';
    import { Text, View, Button, Alert } from 'react-native';
    import {
      NavigationParams,
      NavigationScreenProp,
      NavigationState
    } from 'react-navigation';
    import { CustomHeader } from '../components/Header';
    
    interface Props {
      navigation: NavigationScreenProp<NavigationState, NavigationParams>
    }
    
    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>
        );
      }
    }

    【讨论】:

      【解决方案4】:

      更现代的答案是使用useNavigation,如https://reactnavigation.org/docs/connecting-navigation-prop/ 中所述

      import * as React from 'react';
      import { Button } from 'react-native';
      import { useNavigation } from '@react-navigation/native';
      
      function GoToButton({ screenName }) {
        const navigation = useNavigation();
      
        return (
          <Button
            title={`Go to ${screenName}`}
            onPress={() => navigation.navigate(screenName)}
          />
        );
      }
      

      【讨论】:

      • 谢谢你@TheGoodLife
      • 最好最简单的......但还有一个问题是 screenName 的类型是什么?如何传递网名?
      • 屏幕名称是一个用于导航的反应组件。在学习时,我想出了这个:github.com/marcoXbresciani/TKCompanionApp/tree/master/screens,你可以看看我是如何管理导航和屏幕的。
      【解决方案5】:

      解决这个问题的另一种方法是从 react 中导入 ReactNode,并告诉组件它的类型为 ReactNode。

      import React,{ReactNode} from 'react'
      
      const GetStarted:ReactNode = ({navigation})=>{
      return(
         <View> </View>
      )
      }
      

      【讨论】:

        猜你喜欢
        • 2022-10-16
        • 2020-11-07
        • 2020-01-24
        • 1970-01-01
        • 2021-01-11
        • 1970-01-01
        • 2022-01-23
        • 2020-05-07
        • 2019-12-11
        相关资源
        最近更新 更多