Reactjs 在组件代码的开头调用异步化函数
我正在开发一个本地项目 我有一个功能组件:Reactjs 在组件代码的开头调用异步化函数,reactjs,react-native,Reactjs,React Native,我正在开发一个本地项目 我有一个功能组件: const MyComponent = () => { // the checkData function is an asynchronize function const { checkData } = useContext(MyContext); // I have to wait for the result since the rendering part use the result as you can s
const MyComponent = () => {
// the checkData function is an asynchronize function
const { checkData } = useContext(MyContext);
// I have to wait for the result since the rendering part use the result as you can see below
const isValid = await checkData();
return (<View>
<MySubComponent isValid={isValid}/>
</View>)
}
constmycomponent=()=>{
//checkData函数是一个异步化函数
const{checkData}=useContext(MyContext);
//我必须等待结果,因为渲染部分使用的结果如下所示
const isValid=wait checkData();
返回(
)
}
正如您在上面看到的,我必须调用一个异步函数checkData
&使用结果来呈现MySubComponent
。我不能像上面代码显示的那样使用wait
。我还试着使MyComponent
成为一个异步功能组件,比如const MyComponent=async()=>{…}
,但应用程序崩溃了
在functional component中调用异步函数并使用其结果呈现子组件的正确方法是什么?您可以在函数中调用
wait checkData()
并返回其值,然后调用该函数作为MySubComponent
的属性,如下所示:
const isValid = async () => {
return await checkData();
}
return (
<View>
<MySubComponent isValid={isValid()} />
</View>
);
const isValid=async()=>{
返回等待检查数据();
}
返回(
);
您可以在函数中调用等待checkData()
并返回其值,然后调用该函数作为MySubComponent
的属性,如下所示:
const isValid = async () => {
return await checkData();
}
return (
<View>
<MySubComponent isValid={isValid()} />
</View>
);
const isValid=async()=>{
返回等待检查数据();
}
返回(
);
将其放入useffect
并将isValid
存储在状态
const [isValid, setIsValid] = useState(false);
useEffect(()=> {
const checkIfValid = async () => {
const valid = await checkValid()
setIsValid(valid)
}
checkIfValid();
},[])
将其置于
useffect
中,并将其存储在isValid
状态
const [isValid, setIsValid] = useState(false);
useEffect(()=> {
const checkIfValid = async () => {
const valid = await checkValid()
setIsValid(valid)
}
checkIfValid();
},[])
您可以使用加载async
数据,并使用本地状态跟踪值。为此,您可以尝试useState
hook
将初始值设置为null
,以便在状态等于null
时显示某种加载屏幕。如果没有(这意味着async
进程将值更新为true
或false
),则可以显示组件
const MyComponent = () => {
// the checkData function is an asynchronize function
const { checkData } = useContext(MyContext);
const [isValid, setValidity] = useState(null);
useEffect(() => {
async function init() {
const isValid = await checkData();
setValidity(isValid);
}
init();
}, [])
return (
<View>
{isValid != null ? <MySubComponent isValid={isValid}/> : <SplashScreen />}
</View>)
}
constmycomponent=()=>{
//checkData函数是一个异步化函数
const{checkData}=useContext(MyContext);
常量[isValid,setValidity]=useState(null);
useffect(()=>{
异步函数init(){
const isValid=wait checkData();
setValidity(isValid);
}
init();
}, [])
返回(
{isValid!=null?:}
)
}
您可以使用加载异步
数据,并使用本地状态跟踪值。为此,您可以尝试useState
hook
将初始值设置为null
,以便在状态等于null
时显示某种加载屏幕。如果没有(这意味着async
进程将值更新为true
或false
),则可以显示组件
const MyComponent = () => {
// the checkData function is an asynchronize function
const { checkData } = useContext(MyContext);
const [isValid, setValidity] = useState(null);
useEffect(() => {
async function init() {
const isValid = await checkData();
setValidity(isValid);
}
init();
}, [])
return (
<View>
{isValid != null ? <MySubComponent isValid={isValid}/> : <SplashScreen />}
</View>)
}
constmycomponent=()=>{
//checkData函数是一个异步化函数
const{checkData}=useContext(MyContext);
常量[isValid,setValidity]=useState(null);
useffect(()=>{
异步函数init(){
const isValid=wait checkData();
setValidity(isValid);
}
init();
}, [])
返回(
{isValid!=null?:}
)
}