A set of hooks to help Ionic React developers use native Capacitor APIs and various platform APIs available in Ionic Framework.
This is a new project and we'd love your feedback! Is there a hook that we don't have in here you'd like to see? Or maybe a hook that should function differently than it does today? Let us know by filing an issue!
This is a community-supported add-on to Ionic React. If you'd like to help maintain this repo or have an idea for a hook please file an issue or reach out to the team on Twitter.
This also means the core Ionic React team doesn't guarantee regular updates to this repo, but rather encourages the community to pitch in.
To start using Ionic React Hooks in your app, install the hooks library:
npm install @ionic/react-hooks
Import the hooks from their own path:
import { useStorage } from '@ionic/react-hooks/storage'
Then use the hooks from that namespace in your app:
const [value, setValue] = useStorage('mykey');While Capacitor allows you to write to one API across several platforms, not all features are supported on all platforms. It is encouraged to check if the feature you intend to use is available before using it to avoid any runtime errors.
Each of the hook plugin paths exports an availableFeatures object, which contains a list features for that plugin. If the feature is supported for the current platform the app is running on, that feature will be true.:
const { useStorageItem, availableFeatures } = `@ionic/react-hooks/storage`;
const [value, setValue] = useStorage('mykey');
...
if(availableFeatures.useStorage) {
  setValue('cake');
}Import:
import { useIsScreenReaderEnabled, useSpeak, availableFeatures } from '@ionic/react-hooks/accessibility';useIsScreenReaderEnabled provides access to detecting and responding to a screen reading device or OS setting being enabled:
const {isScreenReaderEnabled} = useIsScreenReaderEnabled();useSpeak activates a text-to-speech engine (if available) to read spoken text.
const { speak } = useSpeak();
speak({value: textToSpeak})Import:
import { useAppState, useAppUrlOpen, useLaunchUrl, availableFeatures } from '@ionic/react-hooks/app';useAppState provides access to App status information, such as whether the app is active or inactive. This value will update dynamically.
const {state} = useAppState();useLaunchUrl provides the URL the app was initially launched with. If you'd like to track future inbound URL events, use useAppUrlOpen below instead.
const { launchUrl } = useLaunchUrl();useAppUrlOpen provides the most recent URL used to activate the app. For example, if the user followed a link in another app that opened your app.
const { appUrlOpen } = useAppUrlOpen();Import:
import { useClose, useOpen, usePrefetch, availableFeatures } from '@ionic/react-hooks/browser';useOpen, usePrefetch, useClose provides a way to launch, prefetch, and close an in-app browser for external content:
useEffect(() => {
  await usePrefetch(['http://ionicframework.com']);
  await useOpen('http://ionicframework.com');
  await useClose();
}, [useOpen, useClose, usePrefetch]);Import:
import { useCamera, availableFeatures } from '@ionic/react-hooks/camera';useCamera provides a way to take a photo:
const { photo, getPhoto } = useCamera();
const triggerCamera = useCallback(async () => {
  getPhoto({
      quality: 100,
      allowEditing: false,
      resultType: CameraResultType.DataUrl
    })
}, [getPhoto]);
<div>{photo && <img alt="" src={photo.dataUrl} />}</div>See the Camera Capacitor API for the options expected.
Import:
import { useClipboard, availableFeatures } from '@ionic/react-hooks/clipboard';useClipboard reads and writes clipboard data:
const { value, getValue, setValue } = useClipboard();
const paste = useCallback(async () => {
  await setValue('http://ionicframework.com/);
}, [setValue]);
const copy = useCallback(async () => {
  getValue(); 
}, [getValue])Import:
import { useGetInfo, useGetLanguageCode, availableFeatures } from '@ionic/react-hooks/device';useGetInfo, useGetLanguageCode gives access to device information and device language settings:
const { info } = useGetInfo();
const { languageCode } = useGetLanguageCode();See the Device Capacitor API for the return type information.
Import:
import { useCurrentPosition, useWatchPosition, availableFeatures } from '@ionic/react-hooks/geolocation';useCurrentPosition returns a single geolocation position using the Geolocation API in Capacitor. The position can be manually updated by calling getPosition:
const { currentPosition, getPosition } = useCurrentPosition();
const handleRefreshPosition = () => {
  getPosition();
}useWatchPosition tracks a geolocation position using the watchPosition in the Geolocation API in Capacitor. The location will automatically begin updating, and you can use the clearWatch and startWatch methods to manually stop and restart the watch.
const { currentPosition, startWatch, clearWatch } = useWatchPosition();See the Geolocation Capacitor API for the options expected.
Import:
import { useStatus, availableFeatures } from '@ionic/react-hooks/network';useStatus monitors network status and information:
 const { networkStatus } = useStatus();See the Network Capacitor API for the type of status.
Import:
import { usePlatform } from '@ionic/react-hooks/platform';usePlatform return the current platform supported by Capacitor. Can be web, ios, android, or electron.
const { platform } = usePlatform();Import:
import { useStorage, useStorageItem, availableFeatures } from '@ionic/react-hooks/storage';useStorage provides access to Capacitor's storage engine. There is also a helper called useStorageItem which makes managing a single item easy if you don't need to access the full Storage API (see below)
const { get, set, remove, getKeys, clear } = useStorage();
useEffect(() => {
  async function example() {
    const value = await get('name');
    await set('name', 'Max');
    await remove('name');
    const allKeys = await getKeys();
    await clear();
  }
}, [ get, set, remove, keys, clear ]);useStorageItem tracks a single item and provides a nice way to read and write that item:
const [ name , setName ] = useStorageItem('name', 'Max');
// Example:
const updateName = useCallback((n) => {
  setName(n);
}, [ setName ]);useStorageItem will use the initial value already in storage, or the one provided if there is no existing value.
