React-Redux and Connect - Why is my state not updating on click?

前端 未结 1 1916
暗喜
暗喜 2021-01-16 00:55

I am new to redux and am writing a simple voting front end which allows the user to vote on their favourite framework (Angular, React, Vue). When a user clicks on a framewor

相关标签:
1条回答
  • 2021-01-16 01:18

    The reason you are not able to the the updated state immediately is because the operation of updating the store is async and writing

    votedAngular(){
        this.props.dispatch(voteAction('angular'));
        console.log(this.props.votes.angular);
    }
    

    means that you are trying to check for the value as soon as the action is triggered. If you want to test the value, I would suggest you do it in the componentWillReceiveProps function

    componentWillReceiveProps(nextProps) {
       console.log(nextProps.votes.angular);
    }
    

    However, for such operations where you need to test for redux values, redux-devtools-extension is an ideal tool.

    0 讨论(0)
提交回复
热议问题