apps/mantine.dev/src/pages/hooks/use-shallow-effect.mdx
import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useShallowEffect);
The use-shallow-effect hook works exactly like useEffect, but performs shallow dependency comparison instead of referential comparison:
import { useEffect } from 'react';
import { useShallowEffect } from '@mantine/hooks';
// Will be called on each render
useEffect(() => {}, [{ a: 1 }]);
// Will be called only once
useShallowEffect(() => {}, [{ a: 1 }]);
The hook works with primitive values, arrays, and objects:
import { useShallowEffect } from '@mantine/hooks';
// Primitive values are handled like in useEffect
useShallowEffect(() => {}, [1, 2, 3]);
// Arrays with primitive values will not trigger callback
useShallowEffect(() => {}, [[1], [2], [3]]);
// Objects with primitive values will not trigger callback
useShallowEffect(() => {}, [{ a: 1 }, { b: 2 }]);
// Arrays with objects will trigger the callback since values are not shallow equal
useShallowEffect(() => {}, [[{ a: 1 }], [{ b: 2 }]]);
function useShallowEffect(
cb: () => void,
dependencies?: React.DependencyList
): void;