score:187
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')
);
score:0
I also write a Login practice. And also meet the same question like you. After a day struggle, I found that only this.props.history.push('/list/')
can make it instead of pulling in a lot of plugins. By the way, the react-router-dom
version is ^4.2.2
. Thanks!
handleSubmit(e){
e.preventDefault();
this.props.form.validateFieldsAndScroll((err,values)=>{
if(!err){
this.setState({
visible:false
});
this.props.form.resetFields();
console.log(values.username);
const path = '/list/';
this.props.history.push(path);
}
})
}
score:0
The below works for me with "react-router@^3.0.5":
package.json:
"react-dom": "^16.6.0",
"react-router": "^3.0.5"
index.js:
import { render } from 'react-dom'
import { App } from './components/App'
import { NotFound404 } from './components/NotFound404'
import { Router, Route, hashHistory } from 'react-router'
render(
<Router history={hashHistory}>
<Route path='/' component={App} />
<Route path='*' component={NotFound404} />
</Router>,
document.getElementById('root')
)
score:0
For (year 2022) version "react-router-dom": "^6.3.0" if anyone is still facing this issue check the order of import in App.js file.
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
I don't know why the order matters but it worked for me.
Also this might help: Attempted import error: 'Switch' is not exported from 'react-router-dom'
score:1
Version 4 of React Router changed several things. They made separate top level router elements for the different history types. If you're using version 4 you should be able to replace <Router history={hashHistory}>
with <HashRouter>
or <BrowserRouter>
.
For more detail, see https://reacttraining.com/react-router/web/guides
score:3
I got the same problem in ES6, but when I switched to use 'react-router-dom' library, the problem was solved. For all fans of ES6, here we go:
npm install --save react-router-dom
In index.js:
import {HashRouter, Route} from 'react-router-dom';
import App from './App';
ReactDOM.render(
<HashRouter>
<Route path="/" component={App}/>
</HashRouter>
,
document.getElementById('root')
);
score:4
If you want to have multiple routes you can use switch like this,
import {Switch} from 'react-router';
then
<BrowserRouter>
<Switch>
<Route exact path="/" component={TodoComponent} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>
score:20
Which version of React Router are you using? Router version 4 changed from passing in the browserHistory class to passing an instance of browserHistory, see the code example in the new docs.
This has been catching lots people who automatically upgrade; a migration document will be out 'any day now'.
You want to add this to the top:
import { createBrowserHistory } from 'history'
const newHistory = createBrowserHistory();
and
<Router history={newHistory}/>
Source: stackoverflow.com
Related Query
- The prop `history` is marked as required in `Router`, but its value is `undefined`. in Router
- Warning: Failed prop type: The prop open is marked as required in Snackbar, but its value is undefined
- Failed prop type: The prop todos[0].id is marked as required in TodoList, but its value is undefined
- Warning: Failed prop type: The prop `store` is marked as required in `Provider`, but its value is `undefined`. in Provider
- The prop `children` is marked as required in `AppBar`, but its value is `undefined`
- The prop `children` is marked as required in `Button`, but its value is `undefined`
- Warning: Failed prop type: The prop `to` is marked as required in `Link`, but its value is `undefined`
- Failed prop type: The prop `userSignUpRequest` is marked as required in `Login`, but its value is `undefined`
- Failed prop type The prop is marked as required but its value is `undefined`
- reactjs Delete Operation Warning: Failed prop type: The prop `role` is marked as required in `ManageRolePage`, but its value is `null`
- Failed prop type: The prop `value` is marked as required in `Rating`, but its value is `undefined`
- Warning: Failed prop type: The prop `value` is marked as required in `TextInput`, but its value is `undefined`
- Warning: Failed prop type: The prop `theme` is marked as required in `MuiThemeProviderOld`, but its value is `undefined`
- Using GatsbyImage - I keep getting an error Failed prop type: The prop `image` is marked as required in `GatsbyImage`, but its value is `undefined`
- Warning: Failed prop type: The prop `term` is marked as required in `ParentComponent`, but its value is `undefined`
- Warning: Failed prop type: The prop `todos[0].title` is marked as required in `TodoList`, but its value is `undefined`
- Failed prop type: The prop `history` is marked as required in `Router`, but its value is `undefined`
- Failed prop type: The prop `children` is marked as required in `Mutation`, but its value is `undefined`
- Failed prop type: The prop `children` is marked as required in` Sidebar`, but its value is `undefined`
- Failed prop type: The prop `id` is marked as required in `FormattedMessage`, but its value is `undefined`
- Warning: Failed prop type: The prop `createCourse` is marked as required in `CoursesPage`, but its value is `undefined`
- Failed prop type: The `` is marked as required in ``, but its value is `undefined`
- Failed prop type: The prop `actions` is marked as required in `Testing`, but its value is `undefined`
- Failed prop type: The prop `location` is marked as required in `PrivateRoute`, but its value is `undefined`
- Warning: Failed prop type: The prop `alt` is marked as required in `MainImage`, but its value is `undefined`. MainImage
- How to fix this issue “Warning: Failed prop type: The prop `title` is marked as required in `Tab`, but its value is `undefined`.”
- Failed prop type: The prop `message` is marked as required in `Description`, but its value is `undefined`
- warning.js:36 Warning: Failed prop type: The prop `history` is marked as required in `Router`, but its value is `undefined`
- Failed prop type: The prop `setToken` is marked as required in `Login`, but its value is `undefined`
- Using .map and getting Warning: Failed prop type: The prop `to` is marked as required in `Link`, but its value is `undefined`
More Query from same tag
- how to pass through parameters using a get/ fetch statement (express)
- useReducer and useEffect hooks to fetch data
- "ReactJs" reset the State but not the unique id
- React - "TypeError: undefined is not a function (near '...formFields.map...')"
- React : How to redirect from Login page to AppLayout page based on status code 200?
- Google Maps element is just grey in react app
- React not displaying anything
- How to change value of parent component's state via the child component?
- React.memo is not available when working with Typescript?
- How to call an API in a conditional with React
- React router Link; activeClassName not working
- Form handling in ReactJS with functional component
- How to test a portion of code that includes a fetch call in JavaScript
- How to get authenticated token inside already logged in page
- How to make a search on an API?
- How to solve Allow access origion problem
- React v15.0.0: Since that renderToString is deprecated, how to go about server-side rendering?
- Update property object in array state reactJS
- How to add a each element of an array into React state
- media query doesn't work properly with css modules when using an arbitrary class name
- Handle Pagination Sorting and Filtering Separately in Antd Table React
- Flattening object and limitting number of nesting
- How to test a component function being called when componentDidMount?
- Select MenuItem doesn't show when JSX saved to state
- How to tackle CORS issues with ReactJS
- ReactJS - setState Axios POST response
- how to setState in componentDidMount in promise without getting setState error
- file upload from reactjs not working. POST call for file upload from reactjs not working
- React-Native Headless doubts
- jsonserver not redirecting using rewriter