React 导入语句的标准方法

IT技术 javascript reactjs typescript import material-ui
2021-05-02 11:58:45

我想知道是否有一种标准方法可以在 react 中编写导入语句?例如,我有这个:

import React, { useState, FormEvent } from 'react';
import Avatar from '@material-ui/core/Avatar';
import {Grid, Checkbox, TextField, FormControlLabel, CssBaseline} from '@material-ui/core';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import { LOGIN } from '../../graphql/mutations/login';
import { schema } from '../../helpers/validations/login';
import { Redirect } from 'react-router-dom';
import { useMutation } from '@apollo/react-hooks';
import StatusMessage from '../../helpers/statusMessages/loginMessage';
import Copyright from '../../components/copyright/copyright';
import CustomButton from '../../components/button/button';
import { ExecutionResult } from 'graphql';
import { Wrapper, StyledLink, Form, StyledTypography, StyledBox, StyledContainer} from './styles';
import { store } from '../../store';
import { useDispatch } from 'react-redux';
import SignInResponse from '../../graphql/responses/login';
import { useFormik } from 'formik';

是否有任何关于我应该'@material-ui/core';单独还是一起导入所有内容的规则除了减少行数之外,它还有什么不同吗?

有没有关于在 React 自己的库/内容之后是否应该导入其他本地文件/函数的规则?任何其他规则/建议?

4个回答

有已知的标准,其中大部分是意见而不是必须做的。我建议您查看eslint-plugin-import,因为它有大量关于导入的标准/意见列表:

  • 确保所有导入出现在其他语句之前 ([ first])
  • 确保所有导出都出现在其他语句 ([ exports-last]) 之后
  • 报告在多个地方重复导入同一module ([ no-duplicates])
  • 禁止命名空间(又名“通配符” *)导入([ no-namespace])
  • 确保在导入路径 ([ extensions]) 中一致使用文件扩展名
  • 在module导入顺序 ([ order]) 中强制执行约定
  • 在导入语句 ([ newline-after-import])后强制换行
  • 如果module导出单个名称 ([ prefer-default-export]),则首选默认导出
  • 限制module可以拥有的最大依赖项数 ([ max-dependencies])
  • 禁止未分配的导入 ([ no-unassigned-import])
  • 禁止命名默认导出 ([ no-named-default])
  • 禁止默认导出 ([ no-default-export])
  • 禁止命名导出 ([ no-named-export])
  • 禁止匿名值作为默认导出 ([ no-anonymous-default-export])
  • 首选将命名导出组合在一个导出声明中 ([ group-exports])
  • 使用 webpackChunkName 为动态导入强制执行前导注释 ([ dynamic-import-chunkname])

关于 order推荐的是:

  1. 节点“内置”module
  2. “外部”module
  3. “内部”module
  4. 来自“父”目录的module
  5. 来自相同或兄弟目录的“兄弟”module
  6. 当前目录的“索引”

不,没有关于如何导入某些内容的标准。但与其导入所有内容,只需导入您需要的内容,它还可以帮助 webpack 对未使用的代码进行摇树。所以我建议这样做:

import { Avatar } from '@material-ui/core';

我喜欢做的另一件事是将我的本地导入与包导入分开,它使代码更具可读性:

import React, { useState, FormEvent } from 'react';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import { ExecutionResult } from 'graphql';
import { Avatar, Grid, Checkbox, TextField, FormControlLabel, CssBaseline } from '@material-ui/core';
import { Redirect } from 'react-router-dom';
import { useMutation } from '@apollo/react-hooks';
import { useDispatch } from 'react-redux';
import { useFormik } from 'formik';

import { LOGIN } from '../../graphql/mutations/login';
import { schema } from '../../helpers/validations/login';
import { store } from '../../store';
import { Wrapper, StyledLink, Form, StyledTypography, StyledBox, StyledContainer } from './styles';
import StatusMessage from '../../helpers/statusMessages/loginMessage';
import Copyright from '../../components/copyright/copyright';
import CustomButton from '../../components/button/button';
import SignInResponse from '../../graphql/responses/login';

没有标准的方法,只是个人喜好。

就个人而言,我更喜欢对来自公共来源的导入进行分组,就像您在'@material-ui/core';. 您也可以使用组件、助手和类似的本地module来做到这一点。此外,我更喜欢先导入第三方module,然后再导入本地module。

这一切都是为了找到一些“合乎逻辑的”并且易于扫描给您的东西。

不要担心使用了多少行来导入module。我认为最好先从其他供应商导入module,然后再导入本地module。我认为有一些lint 规则可以强制执行。

除此之外,我会说只导入需要的东西:

import Avatar from '@material-ui/core/Avatar';

import * as MaterialUI from '@material-ui/core';