2017-04-20 74 views
0

我在使用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,但我不認爲它是一個可靠的選項。

回答