function CategoryCard (props) {
const [done, setDone] = React.useState(null);
let check;
React.useEffect(() => {
async function checkData() {
check = await getData(props.path);
// prints CORRECTLY
console.log(check);
}
checkData();
//prints INCORRECTLY
console.log(check);
setDone(true);
}, []);
return (
<View>
{done ? (
<Text>{check}</Text>
):(
<View style={[styles.container, styles.horizontal]}>
<ActivityIndicator size="large" color="#99ff99" />
</View>
)}
</View>
);
}
如何在React Native中从异步函数中获取值到常规函数?
在上面的代码中,第一个console.log打印了预期的值,但第二个console.log输出undefined,就好像异步函数对变量check没有产生任何影响。
我需要从
getData(props.path)
获取check的值,以便将其显示在