2017-07-29 88 views
0

如何從AsyncStorage將數據加載到應用程序加載第一頁時的reduce store?React Native:從AsyncStorage加載值

我試圖調用componentWillMount中的檢查函數。

componentWillMount(){ 
    debugger; 
    this.check(); 
} 

和在檢查我試圖從AsyncStorage取的值,並將其存儲到終極版存儲對象。

async check(){ 
    await AsyncStorage.getItem('location').then((location) => { 
     this.props.location[0].city = location; 
     } 
    }); 
    this.props.selectLocation(this.props.location); 
} 

由於它是一個異步函數,我無法得到的值,並將其存儲查看終極版selectLocation行動。

如何正確獲取數據並將其存儲在組件安裝之前?

更新:

在我index.android.js我改變了我的店就是這樣,

import { persistStore, autoRehydrate } from 'redux-persist' 
import combineReducers from './src/Redux/reducers/combineReducers'; 
// const store = createStore(combineReducers); 
const store = compose(autoRehydrate())(createStore)(combineReducers) 
persistStore(store, {storage: AsyncStorage},() => { 
    console.log('restored'); 
    console.log(store); 
}) 


export default class ReactNavigation extends Component { 
    render() { 
    return (
     <Provider store = {store}> 
     <App /> 
     </Provider> 
    ); 
    } 
} 
+0

await this.check(); – David

回答

0

你的組件應該通過監聽事件偵聽器來存儲更改並應顯示數據當數據被加載到商店時。

對於目前數據不存在的情況,您應該顯示一些信息,讓用戶知道它正在加載。

理想情況下,您不想讓componentWillMount等待數據加載。

相關問題