asyncstorage + redux thunk + redux persist,调度回调
问题描述:
我在使用redux-thunk的反应原生应用程序中使用了AsyncStorage & redux-persist。所以自然地,在调度一个动作之后,改变状态会有延迟。asyncstorage + redux thunk + redux persist,调度回调
import { compose, createStore, applyMiddleware } from 'redux';
import { persistStore, autoRehydrate } from 'redux-persist';
import thunk from 'redux-thunk';
import reducers from '../reducers';
import { AsyncStorage } from 'react-native';
import createLogger from 'redux-logger';
const logger = createLogger({
predicate:() => process.env.NODE_ENV === 'development'
});
const middleWare = [ thunk, logger ];
const createStoreWithMiddleware = applyMiddleware(...middleWare)(createStore);
export function makeStore(onComplete :?() => void) {
const store = autoRehydrate()(createStoreWithMiddleware)(reducers);
persistStore(store, {
storage: AsyncStorage
}, onComplete);
return store;
}
export default makeStore;
在我的情况
console.log(this.props.counter); // 1
this.props.dispatch(incrementCounter());
console.log(this.props.counter); // still 1 ?
所以我需要做的就是添加一个Promise喜欢上了调度功能类似
this.props.dispatch(incrementCounter())
.then(() => console.log(this.props.counter)); // 2
我怎么能做到这一点的回调?我用setTimeout,但我不认为它是一个可靠的选项。
答
您需要了解背后的基本流量。
行动=>调度=>商店更新=>订阅模块通知
在你的情况下,没有超时的工作,你必须订阅商店购买在商店的更新通知。