reactjs - 如何使用 'withRouter' 和 'withTranslation' 导出 react 组件

标签 reactjs react-router react-i18next react-typescript

我正在尝试使用 withRouter 导出一个类组件和 withTranslation .对于本地化,我正在使用 i18nextreact-i18next .这可以很好地与 javascript react 。但我正在使用与 typescript 的 react 。
成分 -

class TableSorter extends React.Component<any, any> {
  state = {
    data: [],
    pagination: {},
    loading: false,
  };
  componentWillReceiveProps(nextProps: any) {
    nextProps.concussionDataProp.sort(this.compare);
  }
  compare = (a: any, b: any) => {
    ///....
  };

  render() {
    const columns = [
      {
        title: "Firstname",
        dataIndex: "firstName",
        className: "col-first-name",
      },
      {
        title: "Lastname",
        dataIndex: "lastName",
        className: "col-last-name",
      },
      {
        title: "Position",
        dataIndex: "position",
        className: "col-position",
      }
    ];

    return (
      <Table
        columns={columns}
        rowKey={Math.floor(Math.random() * 10).toString()}
        dataSource={this.props.concussionDataProp}
        pagination={false}
        loading={this.state.loading}
        onRow={(record: any) => {
          return {
            onClick: () => {
              this.props.history.push({
                pathname: `/athletes/${record.id}`,
              });
            },
          };
        }}
      />
      //
    );
  }
}

export default withRouter(withTranslation()(TableSorter));
但是编译时出现以下错误。Argument of type 'ComponentType<Pick<any, string | number | symbol>>' is not assignable to parameter of type 'ComponentClass<RouteComponentProps<any, StaticContext, PoorMansUnknown>, any> | FunctionComponent<RouteComponentPro ps<any, StaticContext, PoorMansUnknown>> | (FunctionComponent<...> & ComponentClass<...>) | (ComponentClass<...> & FunctionComponent<...>)'. Type 'ComponentClass<Pick<any, string | number | symbol>, any>' is not assignable to type 'ComponentClass<RouteComponentProps<any, StaticContext, PoorMansUnknown>, any> | FunctionComponent<RouteComponentProps<any, StaticCon text, PoorMansUnknown>> | (FunctionComponent<...> & ComponentClass<...>) | (ComponentClass<...> & FunctionComponent<...>)'. Type 'ComponentClass<Pick<any, string | number | symbol>, any>' is not assignable to type 'ComponentClass<RouteComponentProps<any, StaticContext, PoorMansUnknown>, any>'. Construct signature return types 'Component<Pick<any, string | number | symbol>, any, any>' and 'Component<RouteComponentProps<any, StaticContext, PoorMansUnknown>, any, any>' are incompatible. The types of 'props' are incompatible between these types. Type 'Readonly<Pick<any, string | number | symbol>> & Readonly<{ children?: ReactNode; }>' is not assignable to type 'Readonly<RouteComponentProps<any, StaticContext, PoorMansUnknown>> & Readonly<{ children?: ReactN ode; }>'. Type 'Readonly<Pick<any, string | number | symbol>> & Readonly<{ children?: ReactNode; }>' is missing the following properties from type 'Readonly<RouteComponentProps<any, StaticContext, PoorMansUnknown>>': histor y, location, match谁能帮我这个?

最佳答案

UPD

import React from "react"
import { withTranslation, WithTranslation } from "react-i18next"
import { RouteComponentProps, withRouter } from "react-router"

interface IProps {
    t: (arg: string) => string
}

class TableSorter extends React.Component<IProps & WithTranslation & RouteComponentProps, any> {
    state = {
        data: [],
        pagination: {},
        loading: false,
    }

    render() {
        return ...
    }
}

export default withTranslation()(withRouter(TableSorter))

关于reactjs - 如何使用 'withRouter' 和 'withTranslation' 导出 react 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65032668/

相关文章:

javascript - 找不到模块 'react-i18next'

reactjs - 如何过滤2个日期范围(时间戳)之间的用户数据?

html - 点击打开reactjs sideNav

javascript - 使用 ES6 类时如何使用 mobx.js @observer 将状态连接到 props?

javascript - react js eventListeners 没有按预期删除

internationalization - i18next-browser-languageDetector 路径不工作

javascript - React-hook-form id 未使用 useFieldArray 保存

javascript - 手动刷新或写入时,React-router url 不起作用

javascript - 使用 react-router v4 和预取数据进行服务器渲染

reactjs - 如何正确模拟 i18next