useEventCallback

useCallback without dependencies.

Aimed to be easier to use than useCallback and solve problems raised in this ticket.

  • useEventCallback doesn't need any dependencies list.
  • The returned function should not be used during rendering.

Usage

useEventCallback can be used to have a stable callback without any dependency.

import { useEventCallback } from "swash/utils/useEventCallback";

const Input = () => {
  const [value, setValue] = useState("");
  const onChange = useEventCallback((event) => {
    setValue(event.target.value);
  });
  return <input value={value} onChange={onChange} />;
};