Git Product home page Git Product logo

Comments (2)

zoontek avatar zoontek commented on July 21, 2024 1

Hooks are planned for v5: https://x.com/zoontek/status/1798829383559602444/photo/1
As it requires synchronous check, it can't be added to v4 (it's breaking)

from react-native-permissions.

mjaggard avatar mjaggard commented on July 21, 2024

Something like this:

import React, {useEffect, useState} from 'react';
import {checkMultiple, requestMultiple, RESULTS} from 'react-native-permissions';
import type {Permission} from 'react-native-permissions/src/types.ts';
import {ActivityIndicator, Platform} from 'react-native';

interface Props extends React.PropsWithChildren {
  missing: React.JSX.Element;
  androidPermissions?: Permission[];
  iosPermissions?: Permission[];
}

export default function PermissionsRequired(props: Props): React.JSX.Element {
  const [missingPermissions, setMissingPermissions] = useState<boolean>();

  const permissions = Platform.OS === 'android' ? props.androidPermissions : props.iosPermissions;

  useEffect(() => {
    if (permissions === undefined || permissions.length === 0) {
      setMissingPermissions(false);
      return;
    }

    checkMultiple(permissions)
      .then(r => {
        const failed: Permission[] = [];
        for (let perm of permissions) {
          if (r[perm] !== RESULTS.GRANTED) {
            failed.push(perm);
          }
        }
        if (failed.length > 0) {
          requestMultiple(failed).then(rec => {
            let anyMissing = false;
            for (let perm of permissions) {
              if (rec[perm] !== RESULTS.GRANTED) {
                anyMissing = true;
              }
            }
            setMissingPermissions(anyMissing);
          });
        } else {
          setMissingPermissions(false);
        }
      })
      .catch(e => {
        setMissingPermissions(true); //Should we show a different error here?
      });
  }, []);

  return missingPermissions === undefined ? (
    <ActivityIndicator />
  ) : missingPermissions ? (
    props.missing
  ) : (
    <>{props.children}</>
  );
}

from react-native-permissions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.