简体   繁体   English

使用react-router与typescript

[英]Using react-router with typescript

I'm attempting to port a small react app over to typescript. 我正在尝试将一个小型反应应用程序移植到打字稿上。

I'm encountering issues with react-router. 我遇到了react-router的问题。 I have the latest definitions from definitely type but the following code gives me errors: 我有明确类型的最新定义,但以下代码给出了错误:

var routes:Router.Route = (
<Route name="root" path="/" handler={MyApp}>
  <Route path="dashboard" handler={MyDash} />
  <DefaultRoute handler={SomeSection} />
  <NotFoundRoute handler={NotFoundPage} />
</Route>
);

Router.run(routes, function (Handler:React.Component<any, any>) {
  React.render(<Handler/>, document.body);
});

These are the compilation errors I get: 这些是我得到的编译错误:

js/app.tsx(31,3): error TS2605: JSX element type 'Component<RouteProp, any>' is not a constructor function for JSX elements.
  Property 'render' is missing in type 'Component<RouteProp, any>'.
js/app.tsx(32,5): error TS2605: JSX element type 'Component<RouteProp, any>' is not a constructor function for JSX elements.
js/app.tsx(47,5): error TS2605: JSX element type 'Component<DefaultRouteProp, any>' is not a constructor function for JSX elements.
  Property 'render' is missing in type 'Component<DefaultRouteProp, any>'.
js/app.tsx(49,5): error TS2605: JSX element type 'Component<NotFoundRouteProp, any>' is not a constructor function for JSX elements.
  Property 'render' is missing in type 'Component<NotFoundRouteProp, any>'.
js/app.tsx(53,20): error TS2345: Argument of type '(Handler: Component<any, any>) => void' is not assignable to parameter of type '(Handler: RouteClass, state: RouterState) => void'.
  Types of parameters 'Handler' and 'Handler' are incompatible.
    Type 'Component<any, any>' is not assignable to type 'RouteClass'.
js/app.tsx(54,17): error TS2604: JSX element type 'Handler' does not have any construct or call signatures.

What is the correct way to initialize a set of react-router routes using typescript? 使用typescript初始化一组react-router路由的正确方法是什么?

(I should clarify that I'm using a nightly typescript build which has support for JSX via the --jsx react flag (我应该澄清一下,我正在使用一个夜间打字稿构建,它通过--jsx react flag支持JSX

The root problem was some definitions in react-router not having an explicit render() method. 根本问题是react-router中的一些定义没有显式的render()方法。 This has been fixed (indirectly) by https://github.com/borisyankov/DefinitelyTyped/pull/5197 这已通过https://github.com/borisyankov/DefinitelyTyped/pull/5197 (间接)修复

Note that this code is incorrect: 请注意,此代码不正确:

Router.run(routes, function (Handler:React.Component<any, any>) {
  React.render(<Handler/>, document.body);
});

It should be: 它应该是:

Router.run(routes, function (Handler: new() => React.Component<any, any>) {
  React.render(<Handler/>, document.body);
});

Because Handler is a constructor for a React component, not an instance of it. 因为Handler是React组件的构造函数 ,而不是它的实例。

Errors in the type definitions appears to be the cause. 类型定义中的错误似乎是原因。 You can work around them by modifying the .d.ts files as follows: 您可以通过修改.d.ts文件来解决它们,如下所示:

In react.d.ts , remove render from the JSX.ElementClass interface react.d.ts ,从JSX.ElementClass接口中删除render

interface ElementClass extends React.Component<any, any> {
}

In react-router.d.ts , change run method's routes parameter's type to React.ReactElement<RouteProp> react-router.d.ts ,将run方法的routes参数的类型更改为React.ReactElement<RouteProp>

function run(routes: React.ReactElement<RouteProp>, callback: RouterRunCallback): Router;
function run(routes: React.ReactElement<RouteProp>, location: LocationBase, callback: RouterRunCallback): Router;

For making it running under typescript 1.6 with react-router, I've added the following code in the react-router.d.ts file : 为了让它在带有react-router的typescript 1.6下运行,我在react-router.d.ts文件中添加了以下代码:

interface RouterClass extends React.ComponentClass<any> {}

var Router: RouterClass;

//For Router

function createElement(

type: ReactRouter.RouterClass,

props: any,

...children: __React.ReactNode[]): ReactRouter.Router;

}

interface RouteProp {
    name?: string;
    path?: string;
    handler?: React.ComponentClass<any>;
    ignoreScrollBehavior?: boolean;
    component?: React.ComponentClass<any>;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM