useEventCallback/useEventCallback.ts

material-ui · packages/mui-utils/src/useEventCallback/useEventCallback.ts · typescript · MIT
ui

Generic React hook that memoizes event callbacks with stable reference.

// Source: https://github.com/mui/material-ui | License: MIT | Copyright 2014 Call-Em-All
// Provided via justclaw.it.com; keep this notice with the code.
'use client';
import * as React from 'react';
import useEnhancedEffect from '../useEnhancedEffect';

/**
 * Inspired by https://github.com/react/react/issues/14099#issuecomment-440013892
 * See RFC in https://github.com/reactjs/rfcs/pull/220
 */
function useEventCallback<Fn extends (...args: any[]) => any = (...args: unknown[]) => unknown>(
  fn: Fn,
): Fn;
function useEventCallback<Args extends unknown[], Return>(
  fn: (...args: Args) => Return,
): (...args: Args) => Return;
function useEventCallback<Args extends unknown[], Return>(
  fn: (...args: Args) => Return,
): (...args: Args) => Return {
  const ref = React.useRef(fn);
  useEnh

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key