Skip to content

Commit 2b188cf

Browse files
authored
fix: FIx react act warnings (#103)
1 parent 9c4a87e commit 2b188cf

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/internal/visual-mode/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ export function useDensityMode(elementRef: React.RefObject<HTMLElement>) {
5656
export function useReducedMotion(elementRef: React.RefObject<HTMLElement>) {
5757
const [value, setValue] = useState(false);
5858
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+
}
6071
});
6172
return value;
6273
}

0 commit comments

Comments
 (0)