How to push to History in React Router v4?

后端 未结 21 2176
不思量自难忘°
不思量自难忘° 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条回答
  •  Happy的楠姐
    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'))
    

提交回复
热议问题