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>
);
}
Run Code Online (Sandbox Code Playgroud)
如何从异步函数获取值到反应本机的常规函数中?在上面的代码中,第一个 console.log 打印预期值,但第二个 console.log 给出未定义的值,就好像异步函数从未对变量检查产生任何影响一样。我需要 check from 的值getData(props.path)才能将其显示在<Text>组件中。
有任何想法吗?