Code Monkey home page Code Monkey logo

react-router-dom-example's Introduction

react-router-dom-example

Example usage of react-router-dom.

npm start
npm run build

Demo: https://react-router-dom-example.firebaseapp.com/

One issue that seems common with react-router v4 is the removal of browserHistory. There are several ways to accomplish changing routes from a handler. The way that I found to be the easiest is to use the BrowserRouter which includes the history object in the props passed to the component. Then it is as easy as pushing the new route onto the history. I did not find it necessary to utilize withRouter as some sources suggest.

this.props.history.push(route);

This example shows how to use a HOC to wrap the Routes of components that require authentication.

<Route path="/home" component={restricted(Home)} />

Another challenge I ran into was getting the fallback to a 404 page to work in my development environment using webpack-dev-middleware with express. The solution turned out to be using the connect-history-api-fallback middleware. The webpack-dev-server provides the middleware by default by setting the history-api-fallback property to true, but this shows how to get it working with webpack and express.

import historyApiFallback from 'connect-history-api-fallback';
app.use(historyApiFallback());

So then the Route can be set with no path so that the 404 page will be shown for undefined routes.

<Route component={NotFound} />

TODO: set up unit tests

react-router-dom-example's People

Contributors

dependabot-preview[bot] avatar schnee72 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.