当前位置: 首页 > 知识库问答 >
问题:

如何解决类型“Readonly

蒋英博
2023-03-14

我有以下两段代码:

自定义标题。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,
    },
  });

详细信息屏幕。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中,我得到以下错误:

属性“导航”在“只读”类型上不存在

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

共有3个答案

单于正业
2023-03-14

更现代的答案是使用useNaviation,如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)}
    />
  );
}
岳嘉石
2023-03-14

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

js prettyprint-override">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>
    );
  }
}
窦伟
2023-03-14

我可能是错的,但您是否尝试过添加导航类型以达到预期

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>
    );
  }
}
 类似资料:
  • 我有一个FirebaseActions类,它为我做注册和登录工作。我在其中添加了一个getCurrentUser方法。我从我的主屏幕小部件调用这个函数。我需要将返回值(type= FirebaseUser)放入主屏幕的一个变量中,以访问loggedInUser.email。我的问题;有什么方法可以将FirebaseUser类型数据转换成Future类型变量?当我在HomeScreen而不是Fire

  • 问题内容: 我正在尝试使用WorkManager 1.0.0-alpha09。并得到此错误: 如果我使用1.0.0-alpha08或更低版本。我没有收到此错误,但是我需要公共构造函数 问题答案: 看看https://issuetracker.google.com/issues/116154359。 解决方法是:

  • 我想从listofTaskNotApprove类更新图像,它将documentsnapshot的对象传递到EditTaskNotApprove。在我更新图像之前,我需要显示特定的图像,用户将从列表中选择特定信息。问题是如何在新屏幕上显示图像的当前索引? ListofTasKnot接受类。 这里是EditTask类,我需要显示用户选择的图像的当前索引。 更新图像需要显示的图像这是当用户想要更新Lis

  • 问题内容: 我越来越 非法胶印类型 此代码每次迭代的错误。这是代码: 问题答案: __当您尝试使用 对象 或 数组 作为索引键访问数组索引时,会发生 非法的偏移类型 错误。 例: 您的数组包含一个对象或数组,其值为的某个值,当您尝试将其用作的索引键时,会收到警告。您必须确保包含所需的内容并正确访问它。

  • 当我试图使用angularfire 2为我的离子3应用程序服务时,我再次出现错误。 以下是错误: 类型脚本错误“;”预期。C:/Users/Admin/Verein/node_modules/rxjs/internal/types。d、 ts 导出声明类型ObservedValueOf=O扩展observeInput?T:从来没有; 需要打字错误表达式。C:/用户/管理员/Verein/node_