redux中,当store数据更新后,界面数据并不会直接更新,需要手动更新
如果是函数式组件,手动更新方式如下
// 大坑:redux中,当store数据更新后,界面数据并不会直接更新,需要手动更新
// 初始化一个update数据使用useState(),主要是为了通过setUpdate()来更新组件
// 模拟render()生命周期,实现组件重新加载,以更新界面的store数据
const [update,setUpdate] = useState({})
// useEffect模拟componentDidMount()生命周期
useEffect(() => {
// store.subscribe()是redux提供的,监测store更新的函数
store.subscribe(() => {
// 当store数据更新后执行 setUpdate() ,组件重新加载,实现界面store数据更新
setUpdate({})
})
})
类式组件,使用生命周期直接在store.subscribe()中调用setState()即可实现更新数据
componentDidMount() {
store.subscribe(() => {
this.setState({})
})
}