我正在启动一个新的react 项目,并希望使用最新版本的react 路由.文档建议对所有Web项目使用createBrowserRouter
.他们没有说为什么它比使用BrowserRouter
更好,只是启用了一些数据API.这是唯一的优势吗.
此外,文档中有一个注释,
Due to the decoupling of fetching and rendering in the design of the data APIs, you should create your router outside of the React tree with a statically defined set of routes. For more information on this design, please see the Remixing React Router blog post and the When to Fetch conference talk.
我不太确定他们所说的outside the React tree
并不是我在Reaction树中的项目的全部.
目前我是这样实现我的路由的:
Main.tsx
ReactDOM.createRoot(document.getElementById('root')!).render(
<GenReMsal>
<React.StrictMode>
<App />
</React.StrictMode>
</GenReMsal>
)
App.js
function App({ msalContext }: AppProps) {
return (
<>
<AuthenticatedTemplate>
<Router currentUser={msalContext.accounts[0]} />
</AuthenticatedTemplate>
</>
)
}
Router.js
function Router({ currentUser }: Props) {
const userInfo = { name: currentUser.name }
const router = createBrowserRouter([
{
path: '/:appTypeId?',
element: <AppLayout currentUser={userInfo} />,
loader: defaulNewAPIs,
children: [
{
index: true,
element: <ReferralDetails status="new" />,
},
],
},
])
return <RouterProvider router={router} />
}
这似乎不在react 树之外,但我不完全确定什么会.