File tree 1 file changed +12
-1
lines changed
1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -56,7 +56,18 @@ export function useDensityMode(elementRef: React.RefObject<HTMLElement>) {
56
56
export function useReducedMotion ( elementRef : React . RefObject < HTMLElement > ) {
57
57
const [ value , setValue ] = useState ( false ) ;
58
58
useMutationObserver ( elementRef , node => {
59
- setValue ( isMotionDisabled ( node ) ) ;
59
+ const newValue = isMotionDisabled ( node ) ;
60
+ /**
61
+ * React has a behavior that triggers a re-render even if the same value is provided in the setState, while it does not
62
+ * commit any changes to the DOM (commit phase) the function rerenders. This causes a false react act warnings in testing
63
+ * and any component using the Transition component which in return uses this hook will possibly have false react warnings.
64
+ *
65
+ * To fix this, we manually stop setting the state ourselves if we see the same value.
66
+ * References: https://www.reddit.com/r/reactjs/comments/1ej505e/why_does_it_rerender_even_when_state_is_same/#:~:text=If%20the%20new%20value%20you,shouldn't%20affect%20your%20code
67
+ */
68
+ if ( newValue !== value ) {
69
+ setValue ( newValue ) ;
70
+ }
60
71
} ) ;
61
72
return value ;
62
73
}
You can’t perform that action at this time.
0 commit comments