react router v^4.0.0 Uncaught TypeError: Cannot read property ‘location’ of undefined

You’re doing a few things wrong. First, browserHistory isn’t a thing in V4, so you can remove that. Second, you’re importing everything from react-router, it should be react-router-dom. Third, react-router-dom doesn’t export a Router, instead, it exports a BrowserRouter so you need to import { BrowserRouter as Router } from ‘react-router-dom. Looks like you just … Read more

How to implement authenticated routes in React Router 4?

You’re going to want to use the Redirect component. There’s a few different approaches to this problem. Here’s one I like, have a PrivateRoute component that takes in an authed prop and then renders based on that props. Now your Routes can look something like this If you’re still confused, I wrote this post that may help – Protected routes and … Read more

What is withRouter for in react-router-dom?

When you include a main page component in your app, it is often wrapped in a <Route> component like this: By doing this, the MoviesIndex component has access to this.props.history so it can redirect the user with this.props.history.push. Some components (commonly a header component) appear on every page, so are not wrapped in a <Route>: This means the header cannot redirect the user. … Read more

You should not use Route or withRouter() outside a Router when using react-router 4 and styled-component in react

I’m trying to build my first portfolio website and got stuck in routing using react-router-dom 4.2.2 and styled-components 2.2.3. error message: You should not use Route or withRouter() outside a Router I also try using Link instead of NavLink but got error too(You should not use Link outside a Router) Someone help me please. navigationBar.js navigationBar.style.js

How to use Redirect in the new react-router-dom of Reactjs

You have to use setState to set a property that will render the <Redirect> inside your render() method. E.g. You can also see an example in the official documentation: https://reacttraining.com/react-router/web/example/auth-workflow That said, I would suggest you to put the API call inside a service or something. Then you could just use the history object to … Read more