react-native - React-Native 等待 AsyncStorage.getItem()
问题描述
我面临的问题与以下链接中提到的完全一样:
https://github.com/facebook/react-native/issues/8589
希望从 AsyncStorage 加载并设置一些全局变量。因为其余页面取决于这些变量。
如果我使用async await或then()在设置全局变量之前,我仍然无法阻止其余页面呈现。
我试图让每个组件async,但不幸的是我无法让它正常工作。
主要是因为这一行:
const _App = (props) => {
return (
<I18nextProvider i18n={i18next}>
<App />
</I18nextProvider>);
}
// registerComponent() can't accept anything that is Async
AppRegistry.registerComponent(appName, () => _App);
解决方案
尝试这个
const _App = (props) => {
const [loaded, setLoaded] = useState(false)
useEffect(()=>{
const functionName = async () => {
await registerComponent()
setLoaded(true)
}
functionName()
},[])
if(!loaded) return <View><Text>Some Fancy loading stuff here</Text></View>
return (
<I18nextProvider i18n={i18next}>
<App />
</I18nextProvider>);
}
推荐阅读
- docker - 带有 docker-windows 执行器的 Gitlab 运行器正在拉取 linux docker 映像并失败
- vue.js - 无法将值道具传递给自定义输入组件
- asp.net - .net core 3.1 / .net core 2.1 上的 HttpWebRequest BeginRead 问题
- transactions - 如何使用delphi减少数据集PK字段的序列生成器
- c - 将值附加到结构并打印它
- sass - 对 SCSS @import 路径感到困惑
- javascript - Javascript/c# fullcalendar 打印事件标题+描述
- html - CSS悬停不执行其他元素
- mapper - 带有嵌套对象的 OrikaMapper
- jolt - JOLT 加入不同的属性