react-redux 如何监听state改变

使用redux时 当监听state改变时做出操作

import {connect} from 'react-redux'
import store from "../../rudux/store"


class LayoutMain extends React.Component {
    
    constructor(props){
        super(props)
        store.subscribe(() => {
            console.log('state状态改变了,新状态如下')
            console.log(store.getState())
           this.getMenu()
    
        })
    
    }

}
const mapStateToProps = state =>{

    if(state){

        return{
            menuLength:state
        }
    }

}

export default connect(mapStateToProps)(LayoutMain);
    原文作者:weijie
    原文地址: https://segmentfault.com/a/1190000019236781
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞