【发布时间】:2017-03-09 11:38:33
【问题描述】:
是否有一种既定的方法可以将 redux 状态绑定到对象?
我想做这样的事情:
MyApi.setStore(myReduxStore, 'stateVar')
我玩过传递各种 get/set 操作和存储侦听器,但它一团糟。
MyApi.getState = () => store.dispatch(getAction())
MyApi.setState = (state) => store.dispatch(setAction(state))
let currentState
store.subscribe(() => {
let previousState = currentState
currentState = store.getState().stateVar
if(previousState !== currentState) {
MyApi.stateListener(currentState)
}
})
【问题讨论】:
-
你想达到什么目的? Redux state 已经是对象了...
-
我的 API 做了一些异步操作。我希望它向 redux 公开它的状态,以便可以在各种反应组件中获取它。
-
知道每个reducer都会返回新的状态,你可以创建reducer来从你的API返回状态。
标签: javascript redux react-redux redux-thunk