简体   繁体   English

如何正确使用 ReactJS 到达路由器与 TypeScript 和 ESLint?

[英]How do I correctly use ReactJS Reach Router with TypeScript & ESLint?

I'm trying to implement client-only routes using Reach Router in a project using TypeScript & ESLint.我正在尝试在使用 TypeScript 和 ESLint 的项目中使用 Reach Router 实现仅客户端路由。 This is my initial implementation:这是我最初的实现:

// src/pages/app/[...].tsx
import { Router, Redirect, useLocation } from '@reach/router';
import * as React from 'react';

import BrowseMain from '../../components/BrowseMain';
import Layout from '../../components/Layout';
import UploadMain from '../../components/UploadMain';
import * as styles from '../../styles/[...].module.scss';

const IndexPage = () => {
  const currLocation = useLocation();

  return (
    <Layout>
      <main className={styles.indexMain}>
        <Router basepath="/app">
          {['/', '/browse', '/browse/'].map((path) => <BrowseMain key={path} path={path} />)}
          {['/upload', '/upload/'].map((path) => <UploadMain key={path} path={path} />)}
          {/* Redirect 404 */}
          <Redirect noThrow default from={currLocation.pathname} to="/" />
        </Router>
      </main>
    </Layout>
  );
};

export default IndexPage;

// src/components/BrowseMain.tsx
import * as React from 'react';

import '../styles/BrowseMain.module.scss';
import Main from './Main';

const BrowseMain = () => <Main>Browse</Main>;

export default BrowseMain;

// UploadMain is similar to BrowseMain, so they are facing the same issue here.

// src/components/Main.tsx
import * as React from 'react';

import '../styles/Main.module.scss';

type MainProps = {
  children: string,
};

const Main = ({ children }: MainProps) => <>{children}</>;

export default Main;

At this point, TypeScript threw the following error on the BrowseMain and UploadMain routes in [...].tsx :此时,TypeScript 在[...].tsx中的BrowseMainUploadMain路由上抛出以下错误:

TS2322: Type '{ key: string; TS2322:键入'{键:字符串; path: string;路径:字符串; }' is not assignable to type 'IntrinsicAttributes'. }' 不可分配给类型“IntrinsicAttributes”。 Property 'path' does not exist on type 'IntrinsicAttributes'.类型“IntrinsicAttributes”上不存在属性“路径”。

Following Reach Router's documentation for Usage with TypeScript , I made the following change:按照到达路由器的文档使用 TypeScript ,我做了以下更改:

import { RouteComponentProps } from '@reach/router';
import * as React from 'react';

import '../styles/BrowseMain.module.scss';
import Main from './Main';

const BrowseMain = (props: RouteComponentProps) => <Main>Browse</Main>;

export default BrowseMain;

This solves the previous linting error, however two new ones are raised:这解决了之前的 linting 错误,但是引发了两个新错误:

TS6133: 'props' is declared but its value is never read. TS6133:声明了“道具”,但从未读取其值。

ESLint: 'props' is defined but never used.(@typescript-eslint/no-unused-vars) ESLint:“道具”已定义但从未使用过。(@typescript-eslint/no-unused-vars)

At this point, I'm stuck.在这一点上,我被困住了。 I tried two different things, but they both raised linting errors:我尝试了两种不同的方法,但它们都引发了 linting 错误:

// ESLint: Unexpected empty object pattern.(no-empty-pattern)
const BrowseMain = ({}: RouteComponentProps) => (

// ESLint: '_' is defined but never used.(@typescript-eslint/no-unused-vars)
const BrowseMain = (_: RouteComponentProps) => (

I feel like I'm in a damned if you do, damned if you don't situation right now.我觉得如果你这样做,我就该死,如果你现在不这样做,我就该死。 What's the correct way to declare an explicit type for props that will not be used?为不会使用的道具声明显式类型的正确方法是什么?

You can specify the props using generics.您可以使用 generics 指定道具。 In your case it will look like:在你的情况下,它看起来像:

const BrowserMain: React.FC<RouteComponentProps> = () => <Main>Browse</Main

Using React.FC is however discouraged so you may also do it with a typescript call signature但是不鼓励使用 React.FC,因此您也可以使用 typescript 调用签名

type BrowserMainFC = {
  (props: RouteComponentProps): JSX.Element
}

const BrowseMain: BrowserMainFC = () => <div />

Check out this resource: https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components/查看此资源: https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components/

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

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