React native 反应本机&;异步函数:设置条件值
我试图为iconColor属性返回一个条件值。如果值isFav返回true,我想将iconColor设置为红色,否则它应该为白色。我已经为变量red指定了一个十六进制值,而白色(字符串)由我使用的样式库识别。尽管isFav()返回false(我已经记录了该值),iconColor始终为红色。你知道为什么会这样吗 谢谢你的帮助React native 反应本机&;异步函数:设置条件值,react-native,async-await,callback,conditional-statements,React Native,Async Await,Callback,Conditional Statements,我试图为iconColor属性返回一个条件值。如果值isFav返回true,我想将iconColor设置为红色,否则它应该为白色。我已经为变量red指定了一个十六进制值,而白色(字符串)由我使用的样式库识别。尽管isFav()返回false(我已经记录了该值),iconColor始终为红色。你知道为什么会这样吗 谢谢你的帮助 async function isFav(title) { const value = await AsyncStorage.getItem(title)
async function isFav(title) {
const value = await AsyncStorage.getItem(title)
if (value) {
return true;
} else {
return false;
}
}
function _renderItem({ item }, navigation) {
return (
<TouchableWithoutFeedback onPress={() => navigation.navigate("oneRecipe", {item: item})}>
<View style={styles.recipesItem}>
<CardTextComponent
imageUri={item.image}
title={item.title}
subtitle={`Your Ingredients: ${item.usedIngredientCount}`}
showFavs={true}
iconColor={(async() => isFav()) ? red : "white"}
/>
</View>
</TouchableWithoutFeedback>
);
}
异步函数isFav(标题){
const value=await AsyncStorage.getItem(标题)
如果(值){
返回true;
}否则{
返回false;
}
}
函数_renderItem({item},导航){
返回(
导航(“oneRecipe”,{item:item})}>
isFav())?红色:“白色”}
/>
);
}
为什么不改用状态
const [isFav, setIsFav] = useState(false);
const _isFav = async ()=> {
const value = await AsyncStorage.getItem(title)
if (value) {
return setIsFav(true);
}
return setIsFav(false);
}
useEffect(()=>{
_isFav()
},[])
const _renderItem = ({ item }, navigation)=> {
return (
<TouchableWithoutFeedback onPress={() => navigation.navigate("oneRecipe", {item: item})}>
<View style={styles.recipesItem}>
<CardTextComponent
imageUri={item.image}
title={item.title}
subtitle={`Your Ingredients: ${item.usedIngredientCount}`}
showFavs={true}
iconColor={ isFav ? "red" : "white"}
/>
</View>
</TouchableWithoutFeedback>
);
}
const[isFav,setIsFav]=useState(false);
常量isFav=async()=>{
const value=await AsyncStorage.getItem(标题)
如果(值){
返回setIsFav(true);
}
返回setIsFav(false);
}
useffect(()=>{
_isFav()
},[])
const_renderItem=({item},导航)=>{
返回(
导航(“oneRecipe”,{item:item})}>
);
}