2017-07-29 3 views
0

アプリケーションの最初のページ読み込み時にAsyncStorageからreduxストアにデータをロードするにはどうすればよいですか?React Native:AsyncStorageからのロード値

componentWillMountでチェック機能を呼び出そうとしました。

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

とチェックで、私はAsyncStorageから値を取得し、Reduxのストアオブジェクトにそれを保存しようとしました。

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がデータのロードを待機することは望ましくありません。

関連する問題