typescript 使用 RouteComponentProps 和自定义道具反应路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51523211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
react routing using RouteComponentProps and custom props
提问by DevyDev
Getting crazy about this issue I have..
对这个问题感到疯狂我有..
Firstly, I am still new to React, secondly, before posting, I tried: Passing custom props to router component in react-router v4and react Child component not receiving props
首先,我还是 React 的新手,其次,在发布之前,我尝试过:将 自定义道具传递给 react-router v4 中的路由器组件,并且 反应子组件未接收道具
But no luck..
但没有运气..
what I am trying to achieve, is simply add routing to existing React project.
我想要实现的只是将路由添加到现有的 React 项目。
For example top (father) class looks like this:
例如顶级(父)类看起来像这样:
import * as React from "react";
import { RouteProps } from "react-router";
import { IAppSettings, IAppConfig, IDataAccess } from "../interfaces";
import { AppConfig } from "../appconfig";
import { DataAccess } from "../DataAccess";
import { BookingSiteOverview } from "./BookingSiteOverview";
import { Layout } from "./Layout";
import { NavigationMenu } from "./NavigationMenu";
import { BrowserRouter as Router, Route, Link, Switch, Redirect } from "react-router-dom";
import { RouteComponentProps } from "react-router-dom";
export class CJV extends React.Component<RouteComponentProps<{}>, {}> {
private appConfig: IAppConfig;
private dataAccess: IDataAccess;
constructor() {
super();
const config: IAppSettings = require("Config");
AppConfig.apiUrl = config.ApiUrl;
this.appConfig = new AppConfig();
this.dataAccess = new DataAccess(this.appConfig);
this.dataAccess.getProfiles = this.dataAccess.getProfiles.bind(this);
}
public render() {
return <div>
<Layout>
<NavigationMenu />
<Switch>
<Redirect exact={true} from="/" to="/BookingSiteOverview" />
<Route path="/BookingSiteOverview" exact render={(props) =>
(<BookingSiteOverview getProfiles={this.dataAccess.getProfiles} {...props} />)} />
</Switch>
</Layout>;
</div>;
}
NOTE: error is at getProfiles={this.dataAccess.getProfiles}
Property 'getProfiles' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly>'.
注意:错误是在getProfiles={this.dataAccess.getProfiles}
属性 'getProfiles' 上不存在类型 'Readonly<{ children?: ReactNode; }> & 只读>'。
Child class:
儿童班:
import * as React from "react";
import { ProgressSpinner } from "primereact/components/progressspinner/ProgressSpinner";
import { RouteComponentProps } from "react-router-dom";
import { IBookingSiteOverviewState, IBookingSiteOverviewProps } from "../interfaces";
import { ProfileSelection } from "./ProfileSelection";
import { DateRangeSelection } from "./DateRangeSelection";
import { CollectionStatsTable } from "./CollectionStatsTable";
import { IBookingSiteCollectionStat, ICollectionStatHiddenColumnDefinition } from "../models";
export class BookingSiteOverview extends React.Component<RouteComponentProps<{}>, IBookingSiteOverviewState> {
private stats?: IBookingSiteCollectionStat[];
constructor(props: IBookingSiteOverviewProps) {
super();
this.state = { statsLoading: false, applyButtonPressed: false };
}
public render() {
const statsTable = this.state.statsLoading
? <ProgressSpinner />
: <CollectionStatsTable
id="booking-site-stats"
getRowDefinition={this.props.getProfiles}
//<---- Displays error: Property 'getProfiles' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteComponentProps<{}>>'.
stats={this.stats}
statKey="bookingSiteId"
rowExpansionTemplate={this.props.getProfiles}
tableWidth="1341px" />;
const statsSection = this.state.applyButtonPressed ? statsTable : "";
return <div>
{statsTable}
</div>;
}
}
}
So I cannot figure out, how to pass custom props, and react routing props, so routing would work, and child class receives its properties..
所以我无法弄清楚,如何传递自定义道具,并对路由道具做出反应,因此路由会起作用,并且子类会接收其属性..
回答by Brian Adams
A React component in Typescript is defined like this:
Typescript 中的 React 组件定义如下:
class ComponentName extends React.Component<PropsType, StateType> { ... }
class ComponentName extends React.Component<PropsType, StateType> { ... }
In this case BookingSiteOverview accepts props of type RouteComponentProps<{}> and uses a state of type IBookingSiteOverviewState.
在这种情况下,BookingSiteOverview 接受 RouteComponentProps<{}> 类型的道具并使用 IBookingSiteOverviewState 类型的状态。
RouteComponentProps<{}> does not include a definition for getProfiles, so that is causing the errors.
RouteComponentProps<{}> 不包含 getProfiles 的定义,因此会导致错误。
EDIT
编辑
getProfiles is defined in IBookingSiteOverviewProps (per the OP) so changing the props for BookingSiteOverview to RouteComponentProps<{}> & IBookingSiteOverviewProps
resolves the issue:
getProfiles 在 IBookingSiteOverviewProps(根据 OP)中定义,因此更改 BookingSiteOverview 的道具以RouteComponentProps<{}> & IBookingSiteOverviewProps
解决问题:
export class BookingSiteOverview extends React.Component<RouteComponentProps<{}> & IBookingSiteOverviewProps, IBookingSiteOverviewState> {
constructor(props: RouteComponentProps<{}> & IBookingSiteOverviewProps) {
super(props);
...
}
...
}