score:396
you can use the history
methods outside of your components. try by the following way.
first, create a history
object used the history package:
// src/history.js
import { createbrowserhistory } from 'history';
export default createbrowserhistory();
then wrap it in <router>
(please note, you should use import { router }
instead of import { browserrouter as router }
):
// src/index.jsx
// ...
import { router, route, link } from 'react-router-dom';
import history from './history';
reactdom.render(
<provider store={store}>
<router history={history}>
<div>
<ul>
<li><link to="/">home</link></li>
<li><link to="/login">login</link></li>
</ul>
<route exact path="/" component={homepage} />
<route path="/login" component={loginpage} />
</div>
</router>
</provider>,
document.getelementbyid('root'),
);
change your current location from any place, for example:
// src/actions/useractioncreators.js
// ...
import history from '../history';
export function login(credentials) {
return function (dispatch) {
return loginremotely(credentials)
.then((response) => {
// ...
history.push('/');
});
};
}
upd: you can also see a slightly different example in react router faq.
score:0
you can use it like this as i do it for login and manny different things
class login extends component {
constructor(props){
super(props);
this.login=this.login.bind(this)
}
login(){
this.props.history.push('/dashboard');
}
render() {
return (
<div>
<button onclick={this.login}>login</login>
</div>
)
score:0
/*step 1*/
myfunction(){ this.props.history.push("/home"); }
/**/
<button onclick={()=>this.myfunction()} classname={'btn btn-primary'}>go
home</button>
score:0
create a custom router
with its own browserhistory
:
import react from 'react';
import { router } from 'react-router-dom';
import { createbrowserhistory } from 'history';
export const history = createbrowserhistory();
const extbrowserrouter = ({children}) => (
<router history={history} >
{ children }
</router>
);
export default extbrowserrouter
next, on your root where you define your router
, use the following:
import react from 'react';
import { /*browserrouter,*/ route, switch, redirect } from 'react-router-dom';
//use 'extbrowserrouter' instead of 'browserrouter'
import extbrowserrouter from './extbrowserrouter';
...
export default class root extends react.component {
render() {
return (
<provider store={store}>
<extbrowserrouter>
<switch>
...
<route path="/login" component={login} />
...
</switch>
</extbrowserrouter>
</provider>
)
}
}
finally, import history
where you need it and use it:
import { history } from '../routers/extbrowserrouter';
...
export function logout(){
cleartokens();
history.push('/login'); //works as expected!
return promise.reject('refresh token has expired');
}
score:0
if you want to use history while passing a function as a value to a component's prop, with react-router 4 you can simply destructure the history
prop in the render attribute of the <route/>
component and then use history.push()
<route path='/create' render={({history}) => (
<yourcomponent
yourprop={() => {
this.yourclassmethod()
history.push('/')
}}>
</yourcomponent>
)} />
note: for this to work you should wrap react router's browserrouter component around your root component (eg. which might be in index.js)
score:1
step one wrap your app in router
import { browserrouter as router } from "react-router-dom";
reactdom.render(<router><app /></router>, document.getelementbyid('root'));
now my entire app will have access to browserrouter. step two i import route and then pass down those props. probably in one of your main files.
import { route } from "react-router-dom";
//lots of code here
//somewhere in my render function
<route
exact
path="/" //put what your file path is here
render={props => (
<div>
<nameofcomponent
{...props} //this will pass down your match, history, location objects
/>
</div>
)}
/>
now if i run console.log(this.props) in my component js file that i should get something that looks like this
{match: {…}, location: {…}, history: {…}, //other stuff }
step 2 i can access the history object to change my location
//lots of code here relating to my whatever request i just ran delete, put so on
this.props.history.push("/") // then put in whatever url you want to go to
also i'm just a coding bootcamp student, so i'm no expert, but i know you can also you use
window.location = "/" //wherever you want to go
correct me if i'm wrong, but when i tested that out it reloaded the entire page which i thought defeated the entire point of using react.
score:2
react router v4 now allows the history prop to be used as below:
this.props.history.push("/dummy",value)
the value then can be accessed wherever the location prop is available as
state:{value}
not component state.
score:2
as we have a history already included in react router 5, we can access the same with reference
import react from 'react';
import { browserrouter, switch, route } from 'react-router-dom';
function app() {
const routerref = react.useref();
const onproductnav = () => {
const history = routerref.current.history;
history.push("product");
}
return (
<browserrouter ref={routerref}>
<switch>
<route path="/product">
<productcomponent />
</route>
<route path="/">
<homecomponent />
</route>
</switch>
</browserrouter>
)
}
score:2
in v6, this app should be rewritten to use the navigate api. most of the time this means changing usehistory to usenavigate and changing the history.push or history.replace callsite.
// this is a react router v6 app
import { usenavigate } from "react-router-dom";
function app() {
let navigate = usenavigate();
function handleclick() {
navigate("/home");
}
return (
<div>
<button onclick={handleclick}>go home</button>
</div>
);
}
score:3
use callback. it worked for me!
export function addproduct(props, callback) {
return dispatch =>
axios.post(`${root_url}/cart`, props, config)
.then(response => {
dispatch({ type: types.auth_user });
localstorage.setitem('token', response.data.token);
callback();
});
}
in component, you just have to add the callback
this.props.addproduct(props, () => this.props.history.push('/cart'))
score:4
if you are using redux, then i would recommend using npm package react-router-redux. it allows you to dispatch redux store navigation actions.
you have to create store as described in their readme file.
the easiest use case:
import { push } from 'react-router-redux'
this.props.dispatch(push('/second page'));
second use case with container/component:
container:
import { connect } from 'react-redux';
import { push } from 'react-router-redux';
import form from '../components/form';
const mapdispatchtoprops = dispatch => ({
changeurl: url => dispatch(push(url)),
});
export default connect(null, mapdispatchtoprops)(form);
component:
import react, { component } from 'react';
import proptypes from 'prop-types';
export default class form extends component {
handleclick = () => {
this.props.changeurl('/secondpage');
};
render() {
return (
<div>
<button onclick={this.handleclick}/>
</div>readme file
);
}
}
score:4
here's my hack (this is my root-level file, with a little redux mixed in there - though i'm not using react-router-redux
):
const store = configurestore()
const customhistory = createbrowserhistory({
basename: config.urlbasename || ''
})
reactdom.render(
<provider store={store}>
<router history={customhistory}>
<route component={({history}) => {
window.apphistory = history
return (
<app />
)
}}/>
</router>
</provider>,
document.getelementbyid('root')
)
i can then use window.apphistory.push()
anywhere i want (for example, in my redux store functions/thunks/sagas, etc) i had hoped i could just use window.customhistory.push()
but for some reason react-router
never seemed to update even though the url changed. but this way i have the exact instance react-router
uses. i don't love putting stuff in the global scope, and this is one of the few things i'd do that with. but it's better than any other alternative i've seen imo.
score:4
i was able to accomplish this by using bind()
. i wanted to click a button in index.jsx
, post some data to the server, evaluate the response, and redirect to success.jsx
. here's how i worked that out...
index.jsx
:
import react, { component } from "react"
import { postdata } from "../../scripts/request"
class main extends component {
constructor(props) {
super(props)
this.handleclick = this.handleclick.bind(this)
this.postdata = postdata.bind(this)
}
handleclick() {
const data = {
"first_name": "test",
"last_name": "guy",
"email": "test@test.com"
}
this.postdata("person", data)
}
render() {
return (
<div classname="main">
<button onclick={this.handleclick}>test post</button>
</div>
)
}
}
export default main
request.js
:
import { post } from "./fetch"
export const postdata = function(url, data) {
// post is a fetch() in another script...
post(url, data)
.then((result) => {
if (result.status === "ok") {
this.props.history.push("/success")
}
})
}
success.jsx
:
import react from "react"
const success = () => {
return (
<div classname="success">
hey cool, got it.
</div>
)
}
export default success
so by binding this
to postdata
in index.jsx
, i was able to access this.props.history
in request.js
... then i can reuse this function in different components, just have to make sure i remember to include this.postdata = postdata.bind(this)
in the constructor()
.
score:4
so the way i do it is:
- instead of redirecting using history.push
, i just use redirect
component from react-router-dom
when using this component you can just pass push=true
, and it will take care of the rest
import * as react from 'react';
import { redirect } from 'react-router-dom';
class example extends react.component {
componentdidmount() {
this.setstate({
redirectto: '/test/path'
});
}
render() {
const { redirectto } = this.state;
return <redirect to={{pathname: redirectto}} push={true}/>
}
}
score:5
i struggled with the same topic. i'm using react-router-dom 5, redux 4 and browserrouter. i prefer function based components and hooks.
you define your component like this
import { usehistory } from "react-router-dom";
import { usedispatch } from "react-redux";
const component = () => {
...
const history = usehistory();
dispatch(myactioncreator(othervalues, history));
};
and your action creator is following
const myactioncreator = (othervalues, history) => async (dispatch) => {
...
history.push("/path");
}
you can of course have simpler action creator if async is not needed
score:6
in this case you're passing props to your thunk. so you can simply call
props.history.push('/cart')
if this isn't the case you can still pass history from your component
export function addproduct(data, history) {
return dispatch => {
axios.post('/url', data).then((response) => {
dispatch({ type: types.auth_user })
history.push('/cart')
})
}
}
score:7
this.context.history.push
will not work.
i managed to get push working like this:
static contexttypes = {
router: proptypes.object
}
handlesubmit(e) {
e.preventdefault();
if (this.props.auth.success) {
this.context.router.history.push("/some/path")
}
}
score:7
be careful that don't use react-router@5.2.0
or react-router-dom@5.2.0
with history@5.0.0
. url will update after history.push
or any other push to history instructions but navigation is not working with react-router
. use npm install history@4.10.1
to change the history version. see react router not working after upgrading to v 5.
i think this problem is happening when push to history happened. for example using <navlink to="/apps">
facing a problem in navlink.js that consume <routercontext.consumer>
. context.location
is changing to an object with action and location properties when the push to history occurs. so currentlocation.pathname
is null to match the path.
score:9
i offer one more solution in case it is worthful for someone else.
i have a history.js
file where i have the following:
import createhistory from 'history/createbrowserhistory'
const history = createhistory()
history.pushlater = (...args) => setimmediate(() => history.push(...args))
export default history
next, on my root where i define my router i use the following:
import history from '../history'
import { provider } from 'react-redux'
import { router, route, switch } from 'react-router-dom'
export default class root extends react.component {
render() {
return (
<provider store={store}>
<router history={history}>
<switch>
...
</switch>
</router>
</provider>
)
}
}
finally, on my actions.js
i import history and make use of pushlater
import history from './history'
export const login = createaction(
...
history.pushlater({ pathname: path_redirect_login })
...)
this way, i can push to new actions after api calls.
hope it helps!
score:19
nasty question, took me quite a lot of time, but eventually, i solved it this way:
wrap your container with withrouter
and pass history to your action in mapdispatchtoprops
function. in action use history.push('/url') to navigate.
action:
export function savedata(history, data) {
fetch.post('/save', data)
.then((response) => {
...
history.push('/url');
})
};
container:
import { withrouter } from 'react-router-dom';
...
const mapdispatchtoprops = (dispatch, ownprops) => {
return {
save: (data) => dispatch(savedata(ownprops.history, data))}
};
export default withrouter(connect(mapstatetoprops, mapdispatchtoprops)(container));
this is valid for react router v4.x.
score:23
according to react router v4 documentation - redux deep integration session
deep integration is needed to:
"be able to navigate by dispatching actions"
however, they recommend this approach as an alternative to the "deep integration":
"rather than dispatching actions to navigate you can pass the history object provided to route components to your actions and navigate with it there."
so you can wrap your component with the withrouter high order component:
export default withrouter(connect(null, { actioncreatorname })(reactcomponent));
which will pass the history api to props. so you can call the action creator passing the history as a param. for example, inside your reactcomponent:
onclick={() => {
this.props.actioncreatorname(
this.props.history,
otherparams
);
}}
then, inside your actions/index.js:
export function actioncreatorname(history, param) {
return dispatch => {
dispatch({
type: some_action,
payload: param.data
});
history.push("/path");
};
}
score:27
this is how i did it:
import react, {component} from 'react';
export default class link extends component {
constructor(props) {
super(props);
this.onlogout = this.onlogout.bind(this);
}
onlogout() {
this.props.history.push('/');
}
render() {
return (
<div>
<h1>your links</h1>
<button onclick={this.onlogout}>logout</button>
</div>
);
}
}
use this.props.history.push('/cart');
to redirect to cart page it will be saved in history object.
enjoy, michael.
score:41
simplest way in react router 4 is to use
this.props.history.push('/new/url');
but to use this method, your existing component should have access to history
object. we can get access by
if your component is linked to
route
directly, then your component already has access tohistory
object.eg:
<route path="/profile" component={viewprofile}/>
here
viewprofile
has access tohistory
.if not connected to
route
directly.eg:
<route path="/users" render={() => <viewusers/>}
then we have to use
withrouter
, a heigher order fuction to warp the existing component.inside
viewusers
componentimport { withrouter } from 'react-router-dom';
export default withrouter(viewusers);
that's it now, your
viewusers
component has access tohistory
object.
update
2
- in this scenario, pass all route props
to your component, and then we can access this.props.history
from the component even without a hoc
eg:
<route path="/users" render={props => <viewusers {...props} />}
score:115
now with react-router v5 you can use the usehistory hook like this:
import { usehistory } from "react-router-dom";
function homebutton() {
let history = usehistory();
function handleclick() {
history.push("/home");
}
return (
<button type="button" onclick={handleclick}>
go home
</button>
);
}
read more at: https://reacttraining.com/react-router/web/api/hooks/usehistory
score:389
react router v4 is fundamentally different from v3 (and earlier) and you cannot do browserhistory.push()
like you used to.
this discussion seems related if you want more info:
- creating a new
browserhistory
won't work because<browserrouter>
creates its own history instance, and listens for changes on that. so a different instance will change the url but not update the<browserrouter>
.browserhistory
is not exposed by react-router in v4, only in v2.
instead you have a few options to do this:
use the
withrouter
high-order componentinstead you should use the
withrouter
high order component, and wrap that to the component that will push to history. for example:import react from "react"; import { withrouter } from "react-router-dom"; class mycomponent extends react.component { ... myfunction() { this.props.history.push("/some/path"); } ... } export default withrouter(mycomponent);
check out the official documentation for more info:
you can get access to the
history
object’s properties and the closest<route>
'smatch
via the withrouter higher-order component. withrouter will re-render its component every time the route changes with the same props as<route>
render props:{ match, location, history }
.
use the
context
apiusing the context might be one of the easiest solutions, but being an experimental api it is unstable and unsupported. use it only when everything else fails. here's an example:
import react from "react"; import proptypes from "prop-types"; class mycomponent extends react.component { static contexttypes = { router: proptypes.object } constructor(props, context) { super(props, context); } ... myfunction() { this.context.router.history.push("/some/path"); } ... }
have a look at the official documentation on context:
if you want your application to be stable, don't use context. it is an experimental api and it is likely to break in future releases of react.
if you insist on using context despite these warnings, try to isolate your use of context to a small area and avoid using the context api directly when possible so that it's easier to upgrade when the api changes.
Source: stackoverflow.com
Related Query
- How to push to History in React Router v4?
- How to push to History in React Router v5?
- How can I scroll to a page when doing React Router history push in my case
- How to push to History in React Router Dom v6
- React how to pass the id to router using history push
- How to make react router work with static assets, html5 mode, history API and nested routes?
- how to access history object in new React Router v4
- react router doesn't re-render after history push
- React Router BrowserRouter History Push & Google Analytics
- How to use Browser history in react router with koa
- How to manipulate history in React Router v4?
- React Router navigation using history push
- How can I access the history instance outside of React app with React Router v4?
- React Router -- history push state not refreshing with new state object
- Can't Push History in React Router v4 (no programmatic routing)
- React router history push is ignored by browser back button
- How to access React router history object with component interface that has default custom hook specified
- How to use React Router properly? I am trying to push the state of my application to another page when the user logs in
- GraphQL query does not update on react router v4 history push
- how to define history and match type(from React Router Dom) in React/Typescript
- how can i use history push in useEffect react
- React router history push always route to 404 page
- Onsubmit and history push is not working react router
- React router Prompt history push query params change Prompt location
- How to push history to a specific component by using async fetch in react js
- React Router v4 - How to get current route?
- How to implement authenticated routes in React Router 4?
- How to set the DefaultRoute to another Route in React Router
- How to redirect in React Router v6?
- How to use React Router with Electron?
More Query from same tag
- Animated bar / text with Tailwind CSS and react
- Can't get React state to update
- Searching from State in React
- Select specific checkbox based on index in React using onChange
- How to disable a button when the state or the input value gets changed in React JS?
- How to get data from the backend that needs authorization using React
- Redux const { var1, var2 } = this.props;
- ReactJS How to add a show more/show less button
- Inconsistent values of useState hook
- Set multiple (interdependent) values on state at the same time
- React - Split an attribute to two
- React/redux app renders blank screen on Safari
- React-redux connect HOC as a class decorator, @connect
- How to position this button to the right in this Navbar
- remove multiple object from array and return updated array
- Display Component on click over another Component ReactJS
- Where can I make API call with hooks in react?
- How to override a React component's CSS using emotion CSS?
- Select React Input
- Get a list of files in a react web application
- React javascript syntax error on question mark and dot
- TypeError: is not a function error in child component
- Bootstrap font is different in react
- Return react 16 array elements in typescript
- Click handler on mapboxGL not respondig to updated state using react hooks with typescript
- Can't call client methods from the parent in React?
- Dynamically refreshing the content based on search text
- mdbreact error - Element type is invalid: expected a string (for built-in components)
- React Typescript - ',' expected.ts(1005)
- React + Typescript Higher Order Function "not assignable to type" error