Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@martsie
Created January 31, 2022 02:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martsie/4f3bf6e3c67cf032ba562745a9c161af to your computer and use it in GitHub Desktop.
Save martsie/4f3bf6e3c67cf032ba562745a9c161af to your computer and use it in GitHub Desktop.
useReduceMotion hook for ReactNative
import { useEffect, useState } from 'react'
import { AccessibilityInfo } from 'react-native'
// Adapted from https://github.com/infiniteluke/react-reduce-motion/blob/master/src/targets/native/index.js
export const useReduceMotion = (): boolean => {
const [shouldReduceMotion, setShouldReduceMotion] = useState(false)
useEffect(() => {
const handleChange = (isReduceMotionEnabled: boolean): void => {
setShouldReduceMotion(isReduceMotionEnabled)
}
async function init(): Promise<void> {
handleChange(await AccessibilityInfo.isReduceMotionEnabled())
}
init()
AccessibilityInfo.addEventListener('reduceMotionChanged', handleChange)
return (): void => {
AccessibilityInfo.removeEventListener('reduceMotionChanged', handleChange)
}
}, [])
return shouldReduceMotion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment