【发布时间】:2022-10-11 01:05:14
【问题描述】:
我正在使用reacter-router-dom@6.4.2,我有以下代码:
索引.tsx
const container = document.getElementById("app")!;
const root = createRoot(container);
root.render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter>
<Routes>
<Route path="/" element={<App />} />
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
</Provider>
</React.StrictMode>
);
reportWebVitals();
而在我的应用程序.tsx我有:
const App = () => {
return (
<>
<Sidebar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="patients/:patient" element={<Patient />}>
<Route path="overview" element={<Overview />} />
</Route>
</Routes>
</>
);
};
export default App;
在 Chrome 控制台中,我收到以下警告:
utils.ts:767 You rendered descendant <Routes> (or called `useRoutes()`) at "/" (under <Route path="/">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.
Please change the parent <Route path="/"> to <Route path="*">.
为什么我会收到此警告,我该如何解决?
即使我从App.tsx 中删除<Route path="/" element={<Home />} />,我仍然会收到警告
现在,只有 / 路由有效。它呈现<Sidebar /> 和<Home /> 组件。但是如果我去http://localhost:3000/patients/blah/overview 我得到<NotFound /> 组件,而我应该得到<Overview /> 组件(至少那是我想要得到的)
【问题讨论】:
标签: reactjs typescript react-router react-router-dom