Call function after dispatch from redux has finished

后端 未结 5 595
-上瘾入骨i
-上瘾入骨i 2020-12-14 20:48

All around it but not quite as I have enough of an idea of redux-thunk and of react-router but I am not getting this seemingly simple idea of:

5条回答
  •  别那么骄傲
    2020-12-14 21:38

    I had similar confusion and ended up spending quite a time working on solving the problem through callbacks and javascript promises which is not required at all because of react-redux setup.

    this.props.clearReducersState()
    this.props.history.push('/dashboard')
    

    I was trying to go to my dashboard after my clearReducerState() function is dispatched. This code works just fine.

    You need to do nothing react-redux works on a synchronous way, so you can simply do this. You can use history.push in your action too for that,

    import { withRouter } from 'react-router-dom';
    this.props.clearReducersState(this.props.history)
    
    export default connect(mapStateToProps, mapDispatchToProps)(withRouter(UpdateAid))
    

    And in your action,

    export const clearReducersState = (history) => dispatch => {
    history.push('/dashboard')
    }
    

    However, if you are updating your state using reducers it is better to use history.push in your index file to avoid problems.

    Use as per your requirement. Hope this helps.

提交回复
热议问题