Just upgraded to react-router-dom 4.0.0
. All my components are either regular class
es or fat arrows. They are all exported using export default ThatComponent
. Yet I'm getting this:
Uncaught 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. Check the render method of Router
.
// minimal showcase
import { BrowserRouter, Match, Miss } from 'react-router';
const Router = () => (
<BrowserRouter>
<div>
{/* both Match and Miss components below cause an error */}
<Match exactly pattern="/login" component={Login} />
<Match exactly pattern="/frontpage" component={Frontpage} />
<Match exactly pattern="/logout" render={() => (<div>logout</div>)} />
<Miss component={NoMatch} />
</div>
</BrowserRouter>
);
Why do the <Match>
components think the other components are undefined?
The 'react-router' library is now split into three separate packages. react-router-dom: Designed for web applications. react-router-native: Designed for mobile applications. react-router-core: Can be used anywhere for core applications.
react-router-dom v5 If using RRDv5 if the match prop is undefined this means that BookScreen isn't receiving the route props that are injected when a component is rendered on the Route component's component prop, or the render or children prop functions.
BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.
Both BrowserRouter and HashRouter components were introduced in React Router ver. 4 as subclasses of Router class. Simply, BrowserRouter syncs the UI with the current URL in your browser, This is done by the means of HTML-5 History API. On the other hand, HashRouter uses the Hash part of your URL to sync.
In the final release of react-router
there is no Match
nor Miss
. You just need to use Route
. Also, you need to install and import react-router-dom
package to use BrowserRouter
(see React Router documentation).
To make your example work with minimal changes, you'd need to do the following:
// minimal showcase
import { BrowserRouter, Route, Switch } from 'react-router-dom';
const Router = () => (
<BrowserRouter>
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/frontpage" component={Frontpage} />
<Route exact path="/logout" render={() => (<div>logout</div>)} />
<Route component={NoMatch} />
</Switch>
</BrowserRouter>
);
Please have a look at NoMatchExample in React Router docs to see how to and when to use Switch
.
Check source of react-router here: https://unpkg.com/[email protected]/index.js(also https://unpkg.com/[email protected]/index.js), There is no Match under it. Match maybe belong to other package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With