Develop ultra fast with Prismane 🎉

useMemoization

useMemoization hook provides a simple way to memoize expensive calculations.

Import

import { useMemoization } from "@prismane/core/hooks";

Usage

function Demo() {
  const { memoize, clear } = useMemoization();

  const [value, setValue] = useState("");

  const [computed, setComputed] = useState(0);

  const expensiveFunction = memoize((pk, pv) => {
    setComputed((pc: number) => pc + 1);
    return pv + 1;
  });

  const handleClick = () => {
    const result = expensiveFunction("number", 5);
    setValue(result);
  };

  return (
    <Stack>
      <Text>Value from cache: {value}</Text>
      <Text>Times computed: {computed}</Text>
      <Button onClick={handleClick}>Calculate</Button>
      <Button onClick={() => clear()}>Clear Cache</Button>
    </Stack>
  );
}

API

Return Value

NameTypeDescription
memoize() => voidA function that memoizes the result of the passed value.