Develop ultra fast with Prismane 🎉

usePrevious

usePrevious hook provides a simple way to handle saving the previous value of a variable.

Import

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

Usage

function Demo() {
  const [count, setCount] = useState(0);
  const previous = usePrevious(count);

  const increment = () => {
    setCount(count + 1);
  };

  return (
    <Flex direction="column" gap={fr(2)}>
      <Text
        cl={(theme) => (theme.mode === "dark" ? ["base", 200] : ["base", 700])}
      >
        Current: {count}
      </Text>
      <Text
        cl={(theme) => (theme.mode === "dark" ? ["base", 200] : ["base", 700])}
      >
        Previous: {previous}
      </Text>
      <Button onClick={() => increment()}>Increment</Button>
    </Flex>
  );
}

API

Parameters

NameTypeDescriptionDefault
valueanyThe value that should be tracked.-

Return Value

NameTypeDescription
previousanyThe previous value of the passed variable.