Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The prop `history` is marked as required in `Router`, but its value is `undefined`. in Router

I am new to ReactJs. This is my code:

var React = require('react'); var ReactDOM = require('react-dom'); var {Route, Router, IndexRoute, hashHistory} = require('react-router'); var Main = require('Main'); ReactDOM.render(   <Router history={hashHistory}>   <Route path="/" component={Main}></Route> </Router>, document.getElementById('app')); 

and compiling it with webpack. Also I added Main component to my aliases. The console throws these errors: I also read these links :

React Router failed prop 'history', is undefined

How do I resolve history is marked required, when value is undefined?

Upgrading React-Router and replacing hashHistory with browserHistory

and many searches around the web, but I couldn't fix this issue. React Router is version 4

like image 792
Mammad2c Avatar asked Mar 24 '17 19:03

Mammad2c


People also ask

How do I pass Props to my router?

If you are using react-router v4, you can pass it using the render prop. Sometimes you need to render whether the path matches the location or not. In these cases, you can use the function children prop. It works exactly like render except that it gets called whether there is a match or not.

How do I get past Route response on my router?

To detect previous path in React Router, we can set the state property to an object with the location. pathname as the value. <Link to={{ pathname: "/nextpath", state: { prevPath: location.

What is the difference between BrowserRouter and router?

The main difference between the two is the way they store the URL and communicate with your web server. A <BrowserRouter> uses regular URL paths.

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.


1 Answers

If you are using react-router v4 you need to install react-router-dom as well. After that, import BrowserRouter from react-router-dom and switch Router for BrowserRouter. It seems that v4 change several things. Also, the react-router documentation is outdated. This is my working code:

import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter, Route } from 'react-router-dom' import App from './components/App';  ReactDOM.render((      <BrowserRouter>           <Route path="/" component={App}/>      </BrowserRouter>      ),      document.getElementById('root') ); 

Source

like image 135
betomoretti Avatar answered Oct 15 '22 18:10

betomoretti