How to push to History in React Router v4?

后端 未结 21 2282
不思量自难忘°
不思量自难忘° 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:19

    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')
        })
      }
    }
    

提交回复
热议问题