How to push to History in React Router v4?

后端 未结 21 1875
不思量自难忘°
不思量自难忘° 2020-11-22 00:27

In the current version of React Router (v3) I can accept a server response and use browserHistory.push to go to the appropriate response page. However, this isn

相关标签:
21条回答
  • 2020-11-22 01:01

    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!

    0 讨论(0)
  • 2020-11-22 01:01

    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');
    }
    
    0 讨论(0)
  • 2020-11-22 01:05

    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.

    0 讨论(0)
  • 2020-11-22 01:05

    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.

    0 讨论(0)
  • 2020-11-22 01:09

    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'))
    
    0 讨论(0)
  • 2020-11-22 01:09
    /*Step 1*/
    myFunction(){  this.props.history.push("/home"); }
    /**/
     <button onClick={()=>this.myFunction()} className={'btn btn-primary'}>Go 
     Home</button>
    
    0 讨论(0)
提交回复
热议问题