expo-updates
Version:
Fetches and manages remotely-hosted assets and updates to your app's JS bundle.
49 lines (44 loc) • 1.55 kB
text/typescript
import { useEffect, useRef } from 'react';
import { UpdateEvent } from './Updates.types';
import { addListener } from './UpdatesEmitter';
/**
* @deprecated React hook to create an [`UpdateEvent`](#updateevent) listener subscription on mount, using
* [`addListener`](#updatesaddlistenerlistener). It calls `remove()` on the subscription during unmount.
* This API is deprecated and will be removed in a future release corresponding with SDK 51.
* Use [`useUpdates()`](#useupdates) instead.
*
* @param listener A function that will be invoked with an [`UpdateEvent`](#updateevent) instance
* and should not return any value.
*
* @example
* ```ts
* function App() {
* const eventListener = (event) => {
* if (event.type === Updates.UpdateEventType.ERROR) {
* // Handle error
* } else if (event.type === Updates.UpdateEventType.NO_UPDATE_AVAILABLE) {
* // Handle no update available
* } else if (event.type === Updates.UpdateEventType.UPDATE_AVAILABLE) {
* // Handle update available
* }
* };
* Updates.useUpdateEvents(eventListener);
* // React Component...
* }
* ```
*/
export const useUpdateEvents = (listener: (event: UpdateEvent) => void) => {
const listenerRef = useRef<typeof listener>();
useEffect(() => {
listenerRef.current = listener;
}, [listener]);
useEffect(() => {
if (listenerRef.current) {
const subscription = addListener(listenerRef.current);
return () => {
subscription.remove();
};
}
return undefined;
}, []);
};