Skip to content

useAdaptiveEffect()

const useAdaptiveEffect: (effect, deps?) => void

Defined in: src/hooks/dom/useAdaptiveEffect.ts:22

A hook that adapts to the environment: uses useLayoutEffect on the client side and useEffect on the server side. This ensures compatibility with both SSR and CSR environments.

Inspired by the implementation from usehooks-ts.

Accepts a function that contains imperative, possibly effectful code.

Parameters

effect

EffectCallback

Imperative function that can return a cleanup function

deps?

DependencyList

If present, effect will only activate if the values in the list change.

Returns

void

Version

16.8.0

See

https://react.dev/reference/react/useEffect

See

Example

Just like useEffect, but it chooses the right hook depending on the environment.

tsx
useAdaptiveEffect(() => {
 // Your effect code here
}, [dependencies]);

Released under the MIT License.