react-redux 如何監聽state改變

使用redux時 當監聽state改變時作出操做react

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);
相關文章
相關標籤/搜索