【问题标题】:React TypeScript: Correct types for Route location and children propertiesReact TypeScript:路线位置和子属性的正确类型
【发布时间】:2020-03-19 04:32:41
【问题描述】:

我有一个路由器可以传递位置和子节点的道具,但我不知道这些道具的正确类型是什么。

这是使用 react-router-dom 的路由器...

import React, { useReducer } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { globalContext, setGlobalContext } from './components/context';
import { Layout } from './components/layout';
import { defaultState, globalReducer } from './components/reducer';
import { Home } from './routes/home';
import { NotFound } from './routes/notFound';

const Router: React.FC = () => {
  const [global, setGlobal] = useReducer(globalReducer, defaultState);
  return (
    <setGlobalContext.Provider value={{ setGlobal }}>
      <globalContext.Provider value={{ global }}>
        <BrowserRouter>
          <Route
            render={({ location }) => (
              <Layout location={location}>
                <Switch location={location}>
                  <Route exact path='/' component={Home} />
                  <Route component={NotFound} />
                </Switch>
              </Layout>
            )}
          />
        </BrowserRouter>
      </globalContext.Provider>
    </setGlobalContext.Provider>
  );
};

export { Router };

在布局组件内部,我有一个用于位置和子项的接口,但是因为我不知道这些的正确类型,我最终使用了任何类型,然后我的 linter 就会出现问题。

import React from 'react';

interface PropsInterface {
  children: any;
  location: any;
}

const Layout: React.FC<PropsInterface> = (props: PropsInterface) => {
  return (
    <div>
      <p>{props.location}</p>
      <p>{props.children}</p>
    </div>
  );
};

export { Layout };

我得到的错误是“any”的类型声明失去了类型安全性。考虑用更精确的类型替换它。 (no-any)tslint(1)

我可以从导入import { useLocation } from 'react-router-dom';获取位置

【问题讨论】:

    标签: reactjs typescript react-router-v4


    【解决方案1】:

    您还可以使用react-router-dom 公开的RouteComponentProps 找到这些类型的接口。试试:

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

    并将其映射到您的组件,如下所示:

    // Layout.tsx
    
    import { RouteComponentProps } from "react-router-dom";
    
    const Layout: React.FC<RouteComponentProps> = (props) => {
      return <div>{props.children}</div>;
    };
    

    【讨论】:

      【解决方案2】:

      您正在寻找的Route 的接口可以在@types/react-router 定义中找到。

      在您的项目中安装后,您应该可以使用如下界面来消除该 TS 错误:

      // Layout.tsx
      
      import { RouteProps } from "react-router";
      
      interface ILayoutProps {
        location: RouteProps["location"];
        children: RouteProps["children"];
      }
      
      const Layout: React.FC<ILayoutProps> = (props: ILayoutProps) => {
        return <div>{props.children}</div>;
      };
      

      请记住,location 是一个对象,因此 React 不会让您像以前那样渲染它。

      这是一个工作示例的链接:https://codesandbox.io/s/zealous-snyder-yw9dg


      另一种选择是使用Partial 接口,但这将使所有ILayoutProps 成为可选:

      interface ILayoutProps extends Partial&lt;RouteProps&gt; {}

      【讨论】:

      • 从 'react-router' 导入 { RouteProps };让它对我有用
      • 感谢您的意见!我更新了导入以匹配对您有用的内容。
      猜你喜欢
      • 2023-03-26
      • 2020-03-28
      • 2020-08-24
      • 2021-01-03
      • 2023-03-06
      • 2019-05-10
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      相关资源
      最近更新 更多