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

ReactJS TS,类型“Readonly

缑智敏
2023-03-14

我正在尝试键入我的组件的props并同时使用URL参数。我得到以下错误:

类型“Readonly”上不存在属性“match”

以下是我的一些代码:

import Api from '../../api/Api';

interface MyProps {
    api: Api
}

interface MyState {
    someString: string,
    loading: boolean
}

export class MyComponent extends React.Component<MyProps, MyState> {

    constructor(props: MyProps) {
        super(props);
        this.state = {
            someString: this.props.match.params.someString,//<-- Error is here on this line
            loading: true
        }
    }

    componentDidMount() {
        this.props.api.getSomeInfo(this.state.someString, this.callback)
    }

    callback() {
        let interval = setInterval(function () {
            this.setState({loading: false});
            clearInterval(interval)
        }, 3000);
    }

    render() {
        return (
            <div>
                <p>{this.someString}</p>
            </div>
        );
    }
}

正如你所看到的,我想做的就是:

1-访问:

http://localhost:8080/someStrings/:someString

2-在我的组件的构造函数中获取:一些字符串的值并存储在状态中

3-在我的状态中使用someString的值,以便能够将其作为参数传递给我的API模块来执行操作

4-当回调在API中执行时,我删除了加载动画

我的问题基本上是,我如何声明我的MyProps能够实现这一点?

共有3个答案

胥玮
2023-03-14

尝试将RouteComponentProps接口添加到您的道具中。更改:

export class MyComponent extends React.Component<MyProps, MyState> {

export class MyComponent extends React.Component<MyProps & RouteComponentProps, MyState> {
轩辕经赋
2023-03-14

我就是这样解决的

import {RouteComponentProps} from 'react-router';
interface IMyProps {}

interface IReactRouterParams {
  roomName: string;
  username: string;
}
export class MyComponent extends React.Component<
  IMyProps & RouteComponentProps<IReactRouterParams> {
 
  constructor(props: any) {
    super(props);
    //everything works here
    const {roomName, username} = this.props.match.params;
  }
}
汪才
2023-03-14

这是类型定义中的一个开放问题。https://github.com/DefinitelyTyped/DefinitelyTyped/issues/17355

import { RouteProps } from 'react-router';
import React from 'react';

interface MyProps {
    api: Api
}

interface MyState {
    someString: string,
    loading: boolean
}

class MyComponent extends React.Component<Props & RouteProps, State> // Pay attention here.
{
  // your code...
}

裁判:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/17355#issuecomment-336022780

 类似资料:
  • 前两章讨论了几种保持 DRY 和灵活性的函数式编程技术: 函数组合(function composition) 部分函数应用(partial function application) 柯里化(currying) 这一章依旧围绕代码灵活性而来,不过不再讨论作为头等公民的函数,而是类型系统(注意:并不是要真的去研究类型系统)。 你将学习 类型类 ! 可能你会觉得这没有实际意义,认为这是被 Haske

  • 在尝试将文件转换为打字稿时,我得到了上述错误 我尝试过几种方法,比如向我为这个组件添加的接口添加属性、添加绑定语句、为这些属性添加方法等等。但是它们都会出现错误,通常是上述错误。只需添加打字脚本检查,似乎我不需要做很多更改。我在这里看到的其他类似问题和答案没有帮助。例如,我将这些项目添加到界面道具中,但仍然出现错误。 到目前为止,我拥有的代码(在这次转换之前运行良好)是 我确实有 作为开发依赖项。

  • 当我运行我的代码时,它返回了错误:类型“Future”不是类型转换中“List”类型的子类型。我该如何修复它?它是在我从Firebase获取数据之前匹配引擎运行的?我试图将wait添加到List demoProfiles=getdata()作为wait List;但它返回错误。我真的是新手,这个问题已经困扰了我一整天。请帮帮我

  • 我是新手。我正在开发一个测验应用程序,并拥有以下三个dart文件: 主要的飞奔 question.dart answer.dart 当我在android studio中的android上运行应用程序时,出现以下错误: ══╡ 小部件库捕获的异常╞═══════════════════════════════════════════ 生成MyApp时引发了以下类型的错误(脏,状态:_MyAppSta

  • JPA存储过程参数注释 https://docs.oracle.com/javaee/7/api/javax/persistence/StoredProcedureParameter.html 根据文档,它的类型是JDBC类型。 它应该是Java类型,因为它是类(不是 java.sql.Types.xxx 或JDBCType)?

  • 在存储到本地数据库之前,将字符串转换为对象的正确方法是什么? 这是的输出: 我试图将其转换为CreatedBy对象 创造的 这里是我的本地表列 错误

  • 我正在开发一个依靠API REST调用的flutter应用程序。来自API的响应有点复杂。当调用我的API(例如:api/产品)时,我可以从日志中看到响应:但是我有这个错误: 类型列表 StackTrace: [更新]:退货产品响应。fromJson(response)而不是response ProductsRespository: ProductsBloc: 回应: ApiProvider: 模

  • 我是个新手。当我运行代码时,我得到一个错误:“Future”类型不是“List”类型的子类型。如果我改变屏幕,我将得到错误:NoSuchMethodError:方法“map”被调用为null。我怎样才能解决它?谢谢你帮助我。