@@ -11,29 +11,34 @@ import * as ReactNative from '../react-native';
1111
1212import { useEffect , useState } from 'react' ;
1313
14+ const hasReducedMotionAPI =
15+ ReactNative ?. AccessibilityInfo ?. isReduceMotionEnabled ;
16+
1417export function usePrefersReducedMotion ( ) : boolean {
1518 const [ prefersReducedMotion , setPrefersReducedMotion ] = useState ( false ) ;
1619
1720 useEffect ( ( ) => {
18- // 1. Get the initial value of reduce motion
19- ReactNative . AccessibilityInfo . isReduceMotionEnabled ( ) . then (
20- ( isReduceMotionEnabled ) => {
21- setPrefersReducedMotion ( isReduceMotionEnabled ) ;
22- }
23- ) ;
24-
25- // 2. Subscribe to changes in reduce motion
26- const reduceMotionChangedSubscription =
27- ReactNative . AccessibilityInfo . addEventListener (
28- 'reduceMotionChanged' ,
21+ if ( hasReducedMotionAPI ) {
22+ // 1. Get the initial value of reduce motion
23+ ReactNative . AccessibilityInfo . isReduceMotionEnabled ( ) . then (
2924 ( isReduceMotionEnabled ) => {
3025 setPrefersReducedMotion ( isReduceMotionEnabled ) ;
3126 }
3227 ) ;
3328
34- return ( ) => {
35- reduceMotionChangedSubscription . remove ( ) ;
36- } ;
29+ // 2. Subscribe to changes in reduce motion
30+ const reduceMotionChangedSubscription =
31+ ReactNative . AccessibilityInfo . addEventListener (
32+ 'reduceMotionChanged' ,
33+ ( isReduceMotionEnabled ) => {
34+ setPrefersReducedMotion ( isReduceMotionEnabled ) ;
35+ }
36+ ) ;
37+
38+ return ( ) => {
39+ reduceMotionChangedSubscription . remove ( ) ;
40+ } ;
41+ }
3742 } , [ ] ) ;
3843
3944 return prefersReducedMotion ;
0 commit comments