Skip to content
⚡️React hooks for Ionic React and Capacitor ⚡️
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows Create nodejs.yml Nov 13, 2019
LICENSE License Nov 13, 2019
ionic-react-hooks-gh.png Add files via upload Nov 15, 2019
package-lock.json 0.0.5 release Dec 13, 2019
tsconfig.json fix(): updates for better import from path method Nov 29, 2019

Ionic React Hooks

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!

Support Status

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.

Getting Started

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');

Feature Detection

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) {

Hook Usage

Accessibility Hooks


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

AppState Hooks


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();

Browser Hooks


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(['']);
  await useOpen('');
  await useClose();
}, [useOpen, useClose, usePrefetch]);

Camera Hooks


import { useCamera, availableFeatures } from '@ionic/react-hooks/camera';

useCamera provides a way to take a photo:

const { photo, getPhoto } = useCamera();
const triggerCamera = useCallback(async () => {
      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.

Clipboard Hooks


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(';
}, [setValue]);

const copy = useCallback(async () => {
}, [getValue])

Device Hooks


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.

##Filesystem Hooks


import { useFilesystem, base64FromPath, availableFeatures } from '@ionic/react-hooks/filesystem';

useFilesystem returns back common methods to gain access to file system apis.

const file = await readFile({
  path: filepath,
  directory: FilesystemDirectory.Data

See the Filesystem Capacitor API for description of all the methods and options.

base64FromPath is a helper method that will take in a path to a file and return back the base64 encoded representation of that file.

const base64String = await base64FromPath(path);

Geolocation Hooks


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 = () => {

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.

Network Hooks


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.

Platform Hooks


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();

Storage Hooks


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 ]);

useStorageItem will use the initial value already in storage, or the one provided if there is no existing value.

You can’t perform that action at this time.