I´m trying to use React Router V4 with new React´s context API. Here is my code:
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 contains a router function that will load my screen depending on the navigation path.
module is the module name.
sideMenuConfig is a json configuration for my side menu.
When trying to run I´m getting the following error from react router:
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`.
I expect in Dashboard to receive all the props passed plus the match property to run the router.
AppContext.Providerwith a dot? Also what is the point of rendering aConsumeras an immediate child of aProvider? The error comes from react. Something that you are trying to render is an object. Could you add the stack trace? In which component does it happen?Consumergrabs context from database to offer toProvider. The error seens to be happening inside react-router-v4. No clear stacktrace.<AppContext.Provider>not<AppContextProvider>. But still using the context api like this makes absolutely no sense.