如何将容器组件传递到React-Router-Dom路线中



我当前在传递React Router Redux中正确类型的打字稿问题

打字稿-Error:

severity: 'Error'
message: 'Type '{ path: "/foo/:id"; component: typeof "<path>..' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Route> & Readonly<{ children?: ReactNode; }> & Rea...'.
      Type '{ path: "/foo/:id"; component: typeof "<path>..'  is not assignable to type 'Readonly<RouteProps>'.
        Types of property 'component' are incompatible.
          Type 'typeof "<path>..'  is not assignable to type 'StatelessComponent<RouteComponentProps<any>> | ComponentClass<RouteComponentProps<any>>'.
            Type 'typeof "<path>..'  is not assignable to type 'ComponentClass<RouteComponentProps<any>>'.
              Type 'typeof "<path>..'  provides no match for the signature 'new (props?: RouteComponentProps<any>, context?: any): Component<RouteComponentProps<any>, ComponentState>'.'

这似乎正在发生,因为我将容器组件传递到<Route... />中。但是,我尚不清楚为什么这是不起作用的,因为MapStoprops应该返回RouteComponentProps。

foo.ts(组件):

export interface Props extends RouteComponentProps<any> {
    a: string;
}
export class Foo extends React.Component<Props, void> {
    public render() {
        //code
    }
}

foocontainer.ts:

function mapStateToProps(state: State, ownProps: Props): Props {
    const mappedProps: Props = {
        foo: "super"
    };
    return mappedProps;
}
export default connect(mapStateToProps)(Foo);

路由:

import * as React from "react";
import { Route, Switch } from "react-router-dom";
import FooContainer from "./FooContainer";
export const routes = (
    <Switch>
        <Route path="/foo/:id" component={FooContainer} />
    </Switch>
);

nom库/版本:

 "@types/react": "^16.0.5",
 "@types/react-dom": "^15.5.4",
 "@types/react-redux": "4.4.40",
 "@types/react-router-dom": "4.0.7",
 "@types/react-router-redux": "5.0.8",
 "react": "^15.6.1",
 "react-dom": "^15.6.1",
 "react-redux": "5.0.4",
 "react-router-dom": "4.2.2",
 "react-router-redux": "5.0.0-alpha.6",

如果您查看我的道具,它将扩展RouteComponentProps,即:

export interface RouteComponentProps<P> {
  match: match<P>;
  location: H.Location;
  history: H.History;
}

如果那是真的,那么MapStateToprops不应该包含比赛吗?

,所以我解决了以下问题:

export default connect(mapStateToProps)(foo) as React.ComponentClass<any>;

不确定为什么我需要使用React.componentClass指定返回类型。不是隐式吗?

相关内容

  • 没有找到相关文章

最新更新