score:3
You want to install the history package npm install history
and then pass an instance of it to your router:
import { Router } from "react-router";
import { createBrowserHistory } from "history";
const history = createBrowserHistory()
<Router history={history}>
<App/>
</Router>
Then you can programmatically navigate anywhere in your app by using withRouter
to get your history instance and doing something like this history.push("/my-path")
.
You can also set up a file that creates/exports your history instance that you can just import.
Technically you don't need the history package as react-router will pass its own, but if you're doing this you'll prefer the flexibility that your own history instance will provide.
score:0
Have you tried using Redirect component from router?
This is the example:
<Redirect to="/login" exact component={Login} />
Source: stackoverflow.com
Related Query
- How to programmatically navigate using react router
- Programmatically navigate using React router
- Programmatically navigate using react router V4
- How do you pass data when using the navigate function in react router v6
- Programmatically navigate using react router without duplicating paths
- How do I stop state store data from accumulating in a redux component every time I navigate to it using react router
- How to navigate based on state using react router
- How to trigger popup notification on submit of a form in React using React Router navigate hook?
- How to navigate from one component to another by clicking a button using React router
- How to navigate to other page using react router
- How to click a button and navigate to a new route with URL params using React Router 4
- how to navigate submenu items using React Router Dom v6
- programmatically navigate using react router problem
- How to navigate on path by button click in react router v4?
- How to mock history.push with the new React Router Hooks using Jest
- How to use React Router on top of Rails router (not using react-rails gem)?
- How to pass params into link using React router v6?
- How can I pass data using <Redirect> in react router v4?
- Using React Router and Webpack 2 how to require external libraries only on certain routes?
- How to pass the match when using render in Route component from react router (v4)
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- How to nest Scene and navigate with direction='vertical' in React Native Router Flux?
- how to pass props to the route when using react router <Link>
- Navigate to page after authentication using React Router v4
- How do I programaticly navigate to a route from an event handler in React Router 4.0?
- How we can convert client side rendering react js app to server side rendering using react router 4?
- How to pass data from a page to another page using react router
- How to detect route changes using React Router in React?
- How to refer to data with permalinks using react router
- I can not get the state from react router Link component using useLocation. So how can I pass it?
More Query from same tag
- React & Bootstrap: How to make input box in a PureComponent Card update other instances of itself?
- Simple Ajax request, that loops over data in React.js
- React Testing if Hook State was Updated by a Child Component
- save values of arrays inside a state from different input fields
- How to clear fields after callback from axios?
- How to align a Button to the far right in MUI AppBar?
- Create content to pass to child with event handling in ReactJs
- How to fix Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Dynamically changing the number of rows in a CSS Grid
- How can I affect the state of just one individual component(card) when i have multiple versions of that component?
- Using Props in stateful and Stateless component
- TypeError: Cannot read properties of undefined (reading '0')
- Convert to ISOString when saving on update
- do some works after asynchronous function in redux saga
- React, pass data from child to parent with props
- React, update array's total count state when new item is added to it
- I want to render current timing value to HTML at interval of every seconds in reactjs
- Carousel height adjustment
- Module not found: Can't resolve 'react-pagination'
- Pass Element as a type in function in React/Typescript
- p tag is removing new line character in string
- React.js not re-rendering app on state update
- Call other component's method
- Change series data dynamically in react-highcharts without re-render of the chart
- how to menu item redirect to a link using href?
- How to close bootstrap modal programmatically in react js
- React "Cannot read property 'bind' of undefined"
- React.js Trying to access the response of an API fetch that works and returns data in my /api but I cannot access in my /src/
- How to switch from remote to ipcMain and ipcRenderer?
- Uncaught Invariant Violation: Hooks can only be called inside the body of a function component