@react-native-community/hooks

repository·main·Indexed 25 days ago

https://github.com/react-native-community/hooks

A collection of custom hooks that wrap standard React Native asynchronous APIs for use within functional components. Includes hooks for accessibility information, app state monitoring, hardware back button handling, image dimensions, keyboard visibility, interaction management, device orientation, layout tracking, and pull-to-refresh functionality. Requires React Native version 0.59.0 or newer.

Tokens
2.7K
Snippets
12
Records
22
Agent score
85%

What's inside @react-native-community/hooks

  1. Reference: useImageDimensions types

    main

    The following interfaces define the data structures used by the useImageDimensions hook.

    export interface URISource {
    	uri: string
    }
    
    export type ImageDimensionsSource = ImageRequireSource | URISource
    
    export interface ImageDimensions {
    	width: number
    	height: number
    	aspectRatio: number
    }
    
    export interface ImageDimensionsResult {
    	dimensions?: ImageDimensions
    	error?: Error
    	loading: boolean
    }
  2. Use the useAccessibilityInfo hook

    main

    Access accessibility settings from the device.

    Note: reduceMotionEnabled, grayscaleEnabled, invertColorsEnabled, and reduceTransparencyEnabled require React Native 0.60 or newer.

    import {useAccessibilityInfo} from '@react-native-community/hooks'
    
    const {
      boldTextEnabled,
      screenReaderEnabled,
      reduceMotionEnabled, // requires RN60 or newer
      grayscaleEnabled, // requires RN60 or newer
      invertColorsEnabled, // requires RN60 or newer
      reduceTransparencyEnabled, // requires RN60 or newer
    } = useAccessibilityInfo()
  3. Use the useAppState hook

    main

    Monitor the current application state. The state will change between 'active', 'background', or (on iOS) 'inactive' when the app is closed or moved to the background.

    import {useAppState} from '@react-native-community/hooks'
    
    const currentAppState = useAppState()
  4. Use the useKeyboard hook

    main

    Access information about the keyboard state, including whether it is currently visible and its current height.

    import {useKeyboard} from '@react-native-community/hooks'
    
    const keyboard = useKeyboard()
    
    console.log('keyboard isKeyboardShow: ', keyboard.keyboardShown)
    console.log('keyboard keyboardHeight: ', keyboard.keyboardHeight)
  5. Use the useInteractionManager hook

    main

    Monitor when the InteractionManager has finished all scheduled interactions.

    import {useInteractionManager} from '@react-native-community/hooks'
    
    const interactionReady = useInteractionManager()
    
    console.log('interaction ready: ', interactionReady)
  6. Use the useImageDimensions hook

    main

    Retrieve the dimensions of an image source. Accepts a local required asset or an object with a uri.

    The hook returns an object containing dimensions (with width, height, and aspectRatio), loading (boolean), and error (boolean/error object).

    import {useImageDimensions} from '@react-native-community/hooks'
    
    const source = require('./assets/yourImage.png')
    // or
    const source = {uri: 'https://your.image.URI'}
    
    const {dimensions, loading, error} = useImageDimensions(source)
    
    if (loading || error || !dimensions) {
      return null
    }
    const {width, height, aspectRatio} = dimensions
  7. Use the useRefresh hook

    main

    Implement pull-to-refresh functionality in a ScrollView. Pass an asynchronous function (returning a Promise) to the hook. It returns isRefreshing (boolean) and onRefresh (callback).

    import {useRefresh} from '@react-native-community/hooks'
    
    const fetch = () => {
      return new Promise((resolve) => setTimeout(resolve, 500))
    }
    
    const {isRefreshing, onRefresh} = useRefresh(fetch)
    
    ;<ScrollView
      refreshControl={
        <RefreshControl refreshing={isRefreshing} onRefresh={onRefresh} />
      }
    />
  8. Use the useBackHandler hook

    main

    Handle the hardware back button press. The handler function should return true to consume the event or false to allow the default behavior.

    import {useBackHandler} from '@react-native-community/hooks'
    
    useBackHandler(() => {
      if (shouldBeHandledHere) {
        // handle it
        return true
      }
      // let the default thing happen
      return false
    },[shouldBeHandledHere])
  9. Use the useLayout hook

    main

    Provides an onLayout callback and the resulting layout object for a component.

    import { useLayout } from '@react-native-community/hooks'
    
    const { onLayout, ...layout } = useLayout()
    
    console.log('layout: ', layout)
    
    <View onLayout={onLayout} style={{width: 200, height: 200, marginTop: 30}} />