0

Here is my entry file

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter, Route } from 'react-router-dom'

class Hello extends React.Component {
  render () {
    return <div>Hello!</div>
  }
}

class Goodbye extends React.Component {
  render () {
    return <div>Goodbye!</div>
  }
}

ReactDOM.render(
  <BrowserRouter>
    <div>
      <Route path="/hello" component={Hello} />
      <Route path="/goodbye" component={Goodbye} />
    </div>
  </BrowserRouter>,
  document.getElementById('app')
)

Here is my dependency file

package.json

{
  "name": "React-Router-Redux",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server",
    "build": "webpack -p"
  },
  "keywords": [],
  "author": "Manoj Kumar",
  "license": "ISC",
  "dependencies": {
    "prop-types": "^15.5.10",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-router-dom": "^4.1.2"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "webpack": "^3.5.5",
    "webpack-dev-server": "^2.7.1"
  }
}

When I go http://localhost:8081/hello or http://localhost:8081/goodbye, it renders Cannot GET /hello or Cannot GET /goodbye with this console error

GET http://localhost:8081/hello 404 (Not Found)

or

GET http://localhost:8081/hello 404 (Not Found)

I use react-router-dom^4.1.2, react^15.6.1. I could not solve this please help me.

Thank You

1 Answers1

0

This looks like a Webpack issue. Try adding the line

historyApiFallback: true

to the devServer block of your webpack.config.js. This will redirect the 404s back to /index.html where they should be properly routed by index.js.

Here's a thread discussing an issue similar to yours.

MatTheWhale
  • 967
  • 6
  • 16