2017-07-29 91 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等待数据加载。

相关问题