From d0b46afaa75d695ed3e18b3450af9b17bd1f41c1 Mon Sep 17 00:00:00 2001 From: yyz945947732 <945947732@qq.com> Date: Tue, 29 Aug 2023 15:37:05 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BF=AE=E5=A4=8D=20hooks=20=E5=86=99?= =?UTF-8?q?=E6=B3=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc | 89 ++++++++++++------------ package.json | 1 + src/core/components/RouteGuard/index.tsx | 20 +++--- src/core/hooks/useAuthData.ts | 9 +-- 4 files changed, 60 insertions(+), 59 deletions(-) diff --git a/.eslintrc b/.eslintrc index e9aa316..9ff57a3 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,47 +1,48 @@ { - "env": { - "browser": true, - "es2021": true - }, - "extends": [ - "eslint:recommended", - "plugin:react/recommended", - "plugin:react/jsx-runtime", - "plugin:@typescript-eslint/recommended" - ], - "overrides": [], - "parser": "@typescript-eslint/parser", - "parserOptions": { - "ecmaVersion": "latest", - "sourceType": "module" - }, - "plugins": [ - "react", - "@typescript-eslint", - "unused-imports", - "simple-import-sort" - ], - "rules": { - "no-unused-vars": "off", - "unused-imports/no-unused-imports": "warn", - "unused-imports/no-unused-vars": [ - "warn", - { - "vars": "all", - "varsIgnorePattern": "^_", - "args": "after-used", - "argsIgnorePattern": "^_" - } + "env": { + "browser": true, + "es2021": true + }, + "extends": [ + "eslint:recommended", + "plugin:react/recommended", + "plugin:react/jsx-runtime", + "plugin:@typescript-eslint/recommended", + "plugin:react-hooks/recommended" ], - "simple-import-sort/imports": "warn", - "simple-import-sort/exports": "warn", - "react/react-in-jsx-scope": "off", - "react/prop-types": "off", - "@typescript-eslint/no-explicit-any": "off" - }, - "settings": { - "react": { - "version": "detect" + "overrides": [], + "parser": "@typescript-eslint/parser", + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "plugins": [ + "react", + "@typescript-eslint", + "unused-imports", + "simple-import-sort" + ], + "rules": { + "no-unused-vars": "off", + "unused-imports/no-unused-imports": "warn", + "unused-imports/no-unused-vars": [ + "warn", + { + "vars": "all", + "varsIgnorePattern": "^_", + "args": "after-used", + "argsIgnorePattern": "^_" + } + ], + "simple-import-sort/imports": "warn", + "simple-import-sort/exports": "warn", + "react/react-in-jsx-scope": "off", + "react/prop-types": "off", + "@typescript-eslint/no-explicit-any": "off" + }, + "settings": { + "react": { + "version": "detect" + } } - } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 8226b55..7c38bda 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "coveralls": "^3.1.1", "eslint": "^8.46.0", "eslint-plugin-react": "^7.33.1", + "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-simple-import-sort": "^10.0.0", "eslint-plugin-storybook": "^0.6.13", "eslint-plugin-unused-imports": "^3.0.0", diff --git a/src/core/components/RouteGuard/index.tsx b/src/core/components/RouteGuard/index.tsx index a4bb05a..b0d11ea 100644 --- a/src/core/components/RouteGuard/index.tsx +++ b/src/core/components/RouteGuard/index.tsx @@ -26,20 +26,18 @@ function RouteGuard(props: RouteGuardProps) { const isInRouter = useInRouterContext(); - /* istanbul ignore if */ - if (!isInRouter) { - console.warn( - `请在 react-router-dom V6 的 组件中使用 Auth.${RouteGuard.name} 组件。如果你不是使用 V6 版本请不要使用这个组件。@see https://reactrouter.com/en/main/router-components/router。 - ` - ); - return <>{children}; - } - const location = useLocation(); const allowRoutes = useAuthData(routes, { authKey }); const isAllow = useMemo(() => { - if (!routes) { + /* istanbul ignore if */ + if (!isInRouter) { + console.warn( + `请在 react-router-dom V6 的 组件中使用 Auth.${RouteGuard.name} 组件。如果你不是使用 V6 版本请不要使用这个组件。@see https://reactrouter.com/en/main/router-components/router。 + ` + ); + return true; + } else if (!routes) { return true; } else if (!allowRoutes) { return false; @@ -51,7 +49,7 @@ function RouteGuard(props: RouteGuardProps) { } else { return true; } - }, [location, allowRoutes]); + }, [isInRouter, routes, allowRoutes, location]); return <>{isAllow ? children : NoAuthFallback}; } diff --git a/src/core/hooks/useAuthData.ts b/src/core/hooks/useAuthData.ts index 53e29a0..c41a6bf 100644 --- a/src/core/hooks/useAuthData.ts +++ b/src/core/hooks/useAuthData.ts @@ -27,15 +27,16 @@ const defaultOptions: Required = { /** 获取根据权限过滤的集合 */ export function useAuthData(data?: any[], options?: Options) { - /* istanbul ignore if */ - if (!Array.isArray(data)) { - return data; - } const combineOptions = defaults(options, defaultOptions); const { auth, disabled } = useContext(ProviderContext); const { authProxy } = useContext(ProxyContext); const { validator } = useContext(ValidatorContext); + /* istanbul ignore if */ + if (!Array.isArray(data)) { + return data; + } + function filterFn(item: any) { return ( !item[combineOptions.authKey] ||