简体   繁体   中英

Function called multiple times react native

This is basically a doubt. I am new to React-Native. I have a code something like this:

function MainFunction(){
    console.log("A")
    ....code...
  async function apiFunction(){
    console.log("B")
    ....code...
  }
  useEffect(()=>{
    apiFunction()
  },[])
  return(

     ...JSX code...
  )
}
export default MainFunction;

Now I see that the B is printed only once, which is well and good. But I see that A gets printed multiple times. Is it normal or I messed up with the code? If normal, why so? If messed up, well I will try to fix it.

Thanks

Yes It's normal because every time this component is re-rendered execute all this code, so you will have n console.log('A') where n is re-render times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM