[英]How can I abstract importing routes to the main App.js - React, Node.js
I am currently importing all pages on main App.js file but it became quite messy as there is plenty of imports.我目前正在导入主 App.js 文件上的所有页面,但由于有大量导入,它变得非常混乱。 I import them in the following format:我以以下格式导入它们:
import Account_Dashboard from "./pages/Account_Dashboard";
Then I use them as below:然后我使用它们如下:
<ProtectedRoute
exact
path={PATHS.ACCOUNT_DASHBOARD}
authenticate={this.authenticate}
component={Account_Dashboard}
user={this.state.user}
/>
Is there a way to create separate file where I import all those pages and then I just import that file to make it more dry?有没有办法创建单独的文件,在其中导入所有这些页面,然后只导入该文件以使其更干?
Thanks谢谢
yes, why not, you can create a separate file to manage your routes there, take a look at the below example:是的,为什么不呢,你可以创建一个单独的文件来管理你的路线,看看下面的例子:
RouteProvider.tsx: RouteProvider.tsx:
interface RouteProviderProps {
loading?: JSX.Element;
}
function RouteProvider({ loading }: RouteProviderProps) {
return (
<React.Suspense fallback={loading ? loading : <Loading />}>
<Switch>
<Route exact path="/" component={HomePage} />
<Route path="/search" component={SearchPage} />
<Route exact path="/add" component={Add} />
<Route exact path="/edit/:id" component={Edit} />
<Route path="/relations" component={Relation} />
<Route path="/:slug" component={BlahBlahPage} />
</Switch>
</React.Suspense>
);
}
And now have a simple and good abstraction... so now we use it:现在有了一个简单而好的抽象......所以现在我们使用它:
function App() {
return (
<BrowserRouter>
<ContextProvider>
<RouteProvider />
</ContextProvider>
</BrowserRouter>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.