Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting undefined components in React Router v4

Just upgraded to react-router-dom 4.0.0. All my components are either regular classes 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?

like image 305
Michael Johansen Avatar asked Mar 15 '17 00:03

Michael Johansen


People also ask

What are the components of React Router v4?

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.

Why is match undefined React Router?

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.

What is the BrowserRouter /> component?

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.

What is the difference between HashRouter and BrowserRouter in React?

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.


2 Answers

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.

like image 119
Michał Płachta Avatar answered Nov 06 '22 16:11

Michał Płachta


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.

like image 39
Y.Peng Avatar answered Nov 06 '22 15:11

Y.Peng