Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在组件代码的开头调用异步化函数_Reactjs_React Native - Fatal编程技术网

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?:}
)
}