首页 > 解决方案 > React-Native Text 属性中的异步函数

问题描述

我有一个函数可以从AsyncStorage.

const cloudCheck = async function() {
  let cloud = await AsyncStorage.getItem('cloud');
    cloud = cloud === 'true';
    return cloud;
}

我想用它来做类似的事情,但是因为这是一个我不知道该怎么做的承诺:

<Text className={ cloudCheck == "this" ? "style1" : "stye2" }></Text>

谁能给我任何指导?

标签: react-native

解决方案


你可以在生命周期方法中调用它componentDidUpdate(你也可以,render但最好避免它)生命周期方法,然后setState({ cloud })

之后,在你的render你可以做这样的事情

 const { cloud } = this.state;
 ....
 <Text className={ cloud ? "style1" : "stye2" }></Text>

推荐阅读