无法使用 switch 语句渲染路由 React router dom 5.0.0
Can't render route with switch statements React router dom 5.0.0
我是 React Router 的新手。我正在使用 React-router-dom 5.0.0
我的仪表板路线由于某种原因不匹配,当我导航到 /dashboard
时没有呈现任何内容
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import { MuiThemeProvider } from '@material-ui/core/styles';
import theme from './theme';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';
const MarketPlace = React.lazy(() => import('./marketplace/MarketPlace'));
const Dashboard = React.lazy(() => import('./dashboard/Dashboard'));
const mainApp = (
<MuiThemeProvider theme={theme}>
<BrowserRouter>
<Switch>
<Redirect exact from="/" to="/marketplace" />
<Suspense fallback="Loading...">
<Route component={MarketPlace} exact path="/marketplace" />
</Suspense>
<Suspense fallback="Loading...">
<Route component={Dashboard} exact path="/dashboard" />
</Suspense>
</Switch>
</BrowserRouter>
</MuiThemeProvider>
);
ReactDOM.render(mainApp, document.getElementById('root'));
我做错了什么吗?
Suspense
元素应该在你的开关块之外,像这样:
const App: React.FC = (props) => (
<BrowserRouter>
<React.Suspense fallback="Loading...">
<Switch>
<Route path="/dashboard" component={Dashboard} />
<Route path="/other" component={Other} />
<Redirect to="/dashboard" />
</Switch>
</React.Suspense>
</BrowserRouter>
);
我是 React Router 的新手。我正在使用 React-router-dom 5.0.0
我的仪表板路线由于某种原因不匹配,当我导航到 /dashboard
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import { MuiThemeProvider } from '@material-ui/core/styles';
import theme from './theme';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';
const MarketPlace = React.lazy(() => import('./marketplace/MarketPlace'));
const Dashboard = React.lazy(() => import('./dashboard/Dashboard'));
const mainApp = (
<MuiThemeProvider theme={theme}>
<BrowserRouter>
<Switch>
<Redirect exact from="/" to="/marketplace" />
<Suspense fallback="Loading...">
<Route component={MarketPlace} exact path="/marketplace" />
</Suspense>
<Suspense fallback="Loading...">
<Route component={Dashboard} exact path="/dashboard" />
</Suspense>
</Switch>
</BrowserRouter>
</MuiThemeProvider>
);
ReactDOM.render(mainApp, document.getElementById('root'));
我做错了什么吗?
Suspense
元素应该在你的开关块之外,像这样:
const App: React.FC = (props) => (
<BrowserRouter>
<React.Suspense fallback="Loading...">
<Switch>
<Route path="/dashboard" component={Dashboard} />
<Route path="/other" component={Other} />
<Redirect to="/dashboard" />
</Switch>
</React.Suspense>
</BrowserRouter>
);