javascript - 在反应路由器中分离路由
问题描述
我将 React 与 react-router-dom 一起使用,我想要将我的路由分成不同的文件以获得更清晰的代码。假设我有SettingsRoutes.js
and ChatRoutes.js
。每个都包含相关路由,如下所示:Settings.js
const SettingsRoutes = () => {
const routes = [
{ path: '/help-center', Component: HelpCenter },
{ path: '/contact-us', Component: ContactUs },
{ path: '/terms', Component: TermsOfService },
{ path: '/guidelines', Component: CommunityGuidelines },
{ path: '/policy', Component: PrivacyPolicy },
{ path: '/account', Component: Account },
{ path: '/notifications', Component: Notifications },
{ path: '/report', Component: Report },
{ path: '/settings', Component: Settings },
]
return (
<>
{routes.map(({ path, Component }) => (
<Route path={path} exact render={props => <Component {...props} />} />
))}
</>
)
}
export default SettingsRoutes
聊天.js
import React from 'react'
import { Route, Switch } from 'react-router'
import Inbox from '../container/allChat'
import Chat from '../container/allChat/inbox/Chat'
const ChatRoutes = ({ socket, matches }) => {
const BASE_URL = '/chat'
const baseProps = { socket }
const routes = [
{ path: `${BASE_URL}`, Component: Inbox, ownProps: baseProps },
{
path: `${BASE_URL}/rooms/:roomId/:name/:roomOwnerId`,
Component: matches ? Inbox : Chat,
ownProps: baseProps
},
{ path: `${BASE_URL}/rooms/:mid`, Component: Inbox, ownProps: baseProps }
]
return (
<>
{routes.map(({ path, Component, ownProps }) => (
<Route
path={path}
exact
render={props => <Component {...props} {...ownProps} />}
/>
))}
</>
)
}
export default ChatRoutes
index.js(路由)
<Switch>
<ChatRoutes {...chatProps} />
<SettingsRoutes/>
</Switch>
)
现在由于某种原因只ChatRoutes.js
被渲染但不是Settings.js
我认为它必须与Switch
但不完全确定问题是什么有关。感谢您的帮助。
解决方案
推荐阅读
- erlang - 为什么第二次调用接收没有在 Erlang shell 中检索消息?
- read-the-docs - 尝试在阅读文档时创建 Sphinx 文档时 - 抱怨 libpulse-mainloop-glib.so.0
- c++ - C++20 comparison: warning about ambiguous reversed operator
- sql - 无键加入 - SQL
- reactjs - Cannot type into input field in React
- python - 如何在一个请求中创建多个对象?其中每个对象都有文件字段
- hyperledger-fabric - Hyperledger-fabric2.0 运行第一次网络故障。无法读取“.tar.gz”处的链码包
- hive - Presto 与大表(9 亿条记录)的完全连接比 Hive 慢得多
- c++ - 输入 ID 号以在文本文件中查找
- c# - 考虑不同列上的最高值的记录上的 GroupBy