New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@react-hookbox/immediate-effect

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-hookbox/immediate-effect

To use immediate effect, you need to import the `useImmediateEffectDangerously` hook from the `@react-hookbox/immediate-effect` module.

latest
npmnpm
Version
1.0.1
Version published
Maintainers
0
Created
Source

immediate-effect

To use immediate effect, you need to import the useImmediateEffectDangerously hook from the @react-hookbox/immediate-effect module.

import { useImmediateEffectDangerously } from '@react-hookbox/immediate-effect';

Make sure that all operations inside useImmediateEffectDangerously are synchronous, hence some actions might end up in an error if they cause a re-render.

Hook Code To Copy

import { useRef, EffectCallback } from 'react';

import { assertDepsValidity } from './utils/assert-deps-validity';

export const assertDepsValidity: (deps: unknown) => asserts deps is unknown[] = (deps: unknown) => {
  if (deps === undefined) {
    throw new Error('useImmediateEffect: deps must be provided')
  }

  if (!Array.isArray(deps)) {
    throw new Error('useImmediateEffect: deps must be an array')
  }
};

export const useImmediateEffectDangerously = (
  effectCallback: EffectCallback,
  deps: unknown[]
): void => {
  assertDepsValidity(deps);

  const depsRef = useRef(deps);
  const destructureCallbackRef = useRef<ReturnType<EffectCallback>>();

  if (depsRef.current.length !== deps.length) {
    throw new Error(
      'useImmediateEffectDangerously: deps length must not change'
    );
  }

  if (depsRef.current.every((dep, index) => dep === deps[index])) {
    return;
  }

  depsRef.current = deps;

  destructureCallbackRef.current?.();
  const destructureCallback = effectCallback();
  destructureCallbackRef.current = destructureCallback;
};

FAQs

Package last updated on 10 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts