05.通过subscribe订阅,实现redux数据响应

  1. const store = createStore(weight)
  2. const watch = () => {
  3. const cur = store.getState()
  4. console.log(`stark wang 现在体重为${cur}斤`)
  5. }
  6. store.subscribe(watch)
  7. // watch()
  8. store.dispatch({type:'eat'})
  9. store.dispatch({type:'eat'})
  10. store.dispatch({type:'eat'})
  11. store.dispatch({ type: 'hungry' })

通过 subscribe 订阅一个函数实现数据响应

  1. store.subscribe(watch)