我似乎无法弄清楚如何在 React Router v5 中打印子路由。这是我设置应用程序的方式。
1) index.jsx
ReactDOM.render(
<Provider store={store}>
<IntlProvider defaultLocale="en" locale="en" messages={messages}>
<ThemeProvider theme={theme}>
{Routes()}
</ThemeProvider>
</IntlProvider>
</Provider>,
root,
);
2) 路由.jsx
export default function Routes() {
return (
<ConnectedRouter history={history}>
<Switch>
<Route path="/welcome" component={App} />
<Route component={UnknownPage} />
</Switch>
</ConnectedRouter>
);
}
3) 应用程序.jsx
const App = ({ location }) => (
<div>
<DialogMount />
<RefreshSession />
<Masthead />
<Navigation />
<PageWrapper>
<NavTabs location={location} />
<ContentWrapper>
<Alert />
<Switch>
{generateRoutes(routesConfig)}
</Switch>
</ContentWrapper>
</PageWrapper>
</div>
);
4) generateRoutes 方法
export const generateRoutes = (routes = []) => Object.values(routes).map((route) => {
if (route.redirect) {
return [];
} else if (route.children) {
return (
<Route key={route.path} path={route.path}>
<Switch>
{generateRoutes(route.children)}
</Switch>
</Route>
);
}
return <Route key={route.path} path={route.path} component={route.component} />;
}).reduce((navigation, route) => navigation.concat(route), []);
5) 路由配置
const routesConfig = {
parent: {
path: 'parent',
name: 'parent',
children: {
child1: {
path: 'child1',
name: 'child1',
component: Child1,
},
},
},
};
问题是,从我的 App.jsx 开始,直到呈现 NavTab 之前的所有内容。只是它的路由部分没有被渲染。我知道我在这里遗漏了一些非常愚蠢的东西,但似乎无法弄清楚。
任何帮助表示赞赏。
在 Shubham 的回答之后编辑:
我进行了更改,但仍然面临同样的问题。然而,而不是
render={props => <route.component {...props} />}
我用了
children={props => <route.component {...props} />}
.
这似乎正在加载组件,但现在我看到如下错误:
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `Licensing`.
at createFiberFromTypeAndProps (react-dom.development.js:23965)
at createFiberFromElement (react-dom.development.js:23988)
at createChild (react-dom.development.js:13628)
at reconcileChildrenArray (react-dom.development.js:13900)
at reconcileChildFibers (react-dom.development.js:14305)
at reconcileChildren (react-dom.development.js:16762)
at updateHostComponent (react-dom.development.js:17302)
at beginWork (react-dom.development.js:18627)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)