fix: remove `auth` field, need `next()` for all routes

This commit is contained in:
syy11cn 2021-10-23 00:27:58 +08:00
parent 4115636118
commit 6bd9994084
3 changed files with 23 additions and 39 deletions

View File

@ -1,6 +1,7 @@
{ {
"name": "@syy11cn/config-router", "name": "@syy11cn/config-router",
"version": "1.0.1", "version": "1.0.3",
"description": "A route rendering and route guarding lib.",
"author": { "author": {
"name": "Yiyang Sun", "name": "Yiyang Sun",
"email": "syy11cn@outlook.com" "email": "syy11cn@outlook.com"

View File

@ -18,7 +18,7 @@ const RouterView: FunctionComponent<RouterViewProps> = ({
let redirectPath: string = ''; let redirectPath: string = '';
let agreeToGo = true; let agreeToGo = true;
// Change state of `redirectPath` and `agreeToGo` // Change state of `redirectPath` and `agreeToGo`.
function next(path?: string) { function next(path?: string) {
agreeToGo = true; agreeToGo = true;
if (path) { if (path) {
@ -32,43 +32,27 @@ const RouterView: FunctionComponent<RouterViewProps> = ({
return ( return (
<Switch> <Switch>
{routes.map((route, index) => { {routes.map((route, index) => {
// If auth needed. // If `next()` called.
if (route.auth) { if (agreeToGo) {
// If `next()` called. // Need to redirect.
if (agreeToGo) { if (redirectPath) {
// Need to redirect. return <Redirect key={index} to={redirectPath} />;
if (redirectPath) { } else {
return <Redirect key={index} to={redirectPath} />; // OK to render.
} else { return (
// OK to render. <Route
return ( key={index}
<Route path={route.path}
key={index} render={(props) => (
path={route.path} <route.component
render={(props) => ( {...props}
<route.component // Pass routes of route obj down.
{...props} routes={route.routes}
routes={route.routes} ></route.component>
></route.component> )}
)} ></Route>
></Route> );
);
}
} }
} else {
// No need for auth.
return (
<Route
key={index}
path={route.path}
render={(props) => (
<route.component
{...props}
routes={route.routes}
></route.component>
)}
></Route>
);
} }
})} })}
</Switch> </Switch>

View File

@ -3,7 +3,6 @@ import * as React from 'react';
interface routeType { interface routeType {
path: string; path: string;
component: React.ComponentType<any>; component: React.ComponentType<any>;
auth?: boolean;
exact?: boolean; exact?: boolean;
routes?: Array<routeType> | undefined; routes?: Array<routeType> | undefined;
} }