0

Looking for beginner explanation for the following function that I have seen in an image picker example.

  useEffect(() => {
  (async () => {
    const { status } = await Camera.requestPermissionsAsync();
    setPermission(status === 'granted');
  })();
},[]);

I know the empty array checks for updates to determine whether to re-render. I'm not exactly sure whether () is about the async function or cleaning up, but I know that the picker doesn't work without it.

Kat
  • 105
  • 6

1 Answers1

0

For anyone who's looking:

The async function returns a promise but since useEffect expects a function(or nothing), one must self-invoke the async function. So an easier to read version might look like this -:

 useEffect(() => {
  const askPerm = async () => {
    const { status } = await Camera.requestPermissionsAsync();
    setPermission(status === 'granted');
  };
  askPerm();
},[]);

Kat
  • 105
  • 6