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);

你可能感兴趣的:(redux,react.js)