我正在尝试将 React Router V4 与新的 React 上下文 API 一起使用。这是我的代码:
class App extends Component {
static propTypes = {
router: PropTypes.func.isRequired,
module: PropTypes.string.isRequired,
sideMenuConfig: PropTypes.string
};
render() {
let baseName = "/" + this.props.module;
// This will get my dashboard component, loading context from database
let navComponent = (
<MyCustomAppContextProvider>
<AppContext.Consumer>
{context =>
<Dashboard
context={context}
module={this.props.module}
router={router}
sideMenuConfig={this.props.sideMenuConfig}
/>
}
</AppContext.Consumer>
</MyCustomAppContextProvider>
);
let routes = null;
if (!isAuthenticated()) {
routes = <Auth
baseName={baseName}
/>;
} else {
routes = (
<Switch>
<Route exact path="/logout" component={Logout} />
<Route exact path="/auth" component={Logout} />
<Route exact path="/" component={navComponent} />
<Route
exact
path="/:screen"
component={navComponent}
/>
<Route
exact
path="/:screen/:action"
component={navComponent}
/>
<Route
exact
path="/:screen/:action/:id"
component={navComponent}
/>
<Route component={PageNotFoundError} />
</Switch>
);
}
return (
<BrowserRouter basename={baseName}>
{routes}
</BrowserRouter>
);
}
}
export default App;
router
包含一个路由器功能,它将根据导航路径加载我的屏幕。
module
是模块名称。
sideMenuConfig
是我的侧边菜单的 json 配置。
尝试运行时,我从反应路由器收到以下错误:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
Check the render method of `Route`.
我希望在仪表板中接收所有传递的道具以及匹配属性来运行路由器。