使用“ withRouter”和Typescript时类型出现问题

P.G*_*cia 3 typescript react-router react-router-dom

我正在尝试对React + Typescript进行更深入的了解和实践,使用withRouterfrom 时遇到了这种输入错误react-router-dom

我的代码段非常简单,我尝试找出存在相同问题的人员,并且一些答案指出升级时出错(但它们来自2016年,所以...),其中一些正在使用一条connect()我没有使用的陈述(这导致了一个问题:“由于未使用,我做错了吗?”)。我看到其中一些建议还涉及将“ Props”映射到“ State”,到目前为止,我还没有做过(也没有看到)。我希望有人对我所缺少的内容以及我应该关注的内容提出一些建议。

代码是:

import React from "react";
import { withRouter } from "react-router-dom";

interface ISection {
  id: number;
  title: string;
  imageUrl: string;
  size: string;
}

class MenuItem extends React.Component<ISection> {
  render() {
    return (
      <div className={`${this.props.size} menu-item`}>
        <div
          className="background-image"
          style={{ backgroundImage: `url(${this.props.imageUrl})` }}
        />
        <div className="content">
          <h1 className="title">{this.props.title}</h1>
          <span className="subtitle">some subtitle</span>
        </div>
      </div>
    );
  }
}

export default withRouter(MenuItem);
Run Code Online (Sandbox Code Playgroud)

从这里我期望可以平稳地工作(我不得不说我首先尝试了一个功能组件,因为我没有任何状态,但是我看到的所有解决方案都涉及一个类组件,因此我将其移入了该组件中。 ),但MenuItem在最后一行的上却出现以下错误:

Argument of type 'typeof MenuItem' is not assignable to parameter of type 'ComponentClass<RouteComponentProps<any, StaticContext, any>, any> | FunctionComponent<RouteComponentProps<any, StaticContext, any>> | (FunctionComponent<RouteComponentProps<any, StaticContext, any>> & ComponentClass<...>) | (ComponentClass<...> & FunctionComponent<...>)'.
  Type 'typeof MenuItem' is not assignable to type 'ComponentClass<RouteComponentProps<any, StaticContext, any>, any>'.
    Types of parameters 'props' and 'props' are incompatible.
      Type 'RouteComponentProps<any, StaticContext, any>' is missing the following properties from type 'Readonly<ISection>': id, title, imageUrl, sizets(2345)
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 为什么说“类型'typeof MenuItem'”?它不应该只说'MenuItem'的类型,而不是说要获取类型的函数吗?

  2. withRouter是否需要使用类组件,或者也可以使用功能组件?

  3. 我需要connect()一些东西,还是将道具映射到State?如果是这样,为什么?

  4. 最后,我该如何解决?

Fyo*_*dor 8

文档开始,无论何时呈现,withRouter都会将update matchlocationhistoryprops传递给包装的组件。

因此MenuItem组件应该有道具来接收它们。目前,MenuItem组件的道具类型ISection不包括路由器道具。

添加路由器道具最简单的方法就是交叉ISection使用RouteComponentProps

import { withRouter, RouteComponentProps } from "react-router-dom";

// ...
class MenuItem extends React.Component<ISection & RouteComponentProps> {
Run Code Online (Sandbox Code Playgroud)

完整的代码是

import * as React from 'react';
import { withRouter, RouteComponentProps } from "react-router-dom";

interface ISection {
    id: number;
    title: string;
    imageUrl: string;
    size: string;
}

class MenuItem extends React.Component<ISection & RouteComponentProps> {
    render() {
        return (
            <div className={`${this.props.size} menu-item`}>
                <div
                    className="background-image"
                    style={{ backgroundImage: `url(${this.props.imageUrl})` }}
                />
                <div className="content">
                    <h1 className="title">{this.props.title}</h1>
                    <span className="subtitle">some subtitle</span>
                </div>
            </div>
        );
    }
}

export default withRouter(MenuItem);
Run Code Online (Sandbox Code Playgroud)

并回答您的问题

  1. 为什么说“类型'typeof MenuItem'”?它不应该只说'MenuItem'的类型,而不是说要获取类型的函数吗?

    类型不兼容引起错误。MenuItem是班级,而不是类型。要获得MenuItem您的类型,请使用typeof MenuItem。所以,typeof MenuItem是的类型。编译器正确地说“类型typeof MenuItem”。

  2. withRouter是否需要使用类组件,或者也可以使用功能组件?

    允许它与类组件和功能组件一起使用。

    如果实现为功能,这就是您的组件的外观

    const Cmp1: React.FunctionComponent<ISection & RouteComponentProps> = (props) => {
        return (
            <div className={`${props.size} menu-item`}>
                <div
                    className="background-image"
                    style={{ backgroundImage: `url(${props.imageUrl})` }}
                />
                <div className="content">
                    <h1 className="title">{props.title}</h1>
                    <span className="subtitle">some subtitle</span>
                </div>
            </div>
        );
    }
    
    const WrappedCmp = withRouter(Cmp1);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我需要connect()一些东西,还是将道具映射到State?如果是这样,为什么?

    不,这不是严格的要求。connect是Redux的一部分,因此,如果您使用Redux,则可以连接。下面是文档如何使用withRouterconnect。但是同样,它不是必需的。

  4. 最后,我该如何解决?

    已经回答。往上看 :-)