diff --git a/src/useCustomCompareEffect/__docs__/example.stories.tsx b/src/useCustomCompareEffect/__docs__/example.stories.tsx index da55ded9..9a1e1774 100644 --- a/src/useCustomCompareEffect/__docs__/example.stories.tsx +++ b/src/useCustomCompareEffect/__docs__/example.stories.tsx @@ -1,15 +1,96 @@ import * as React from 'react'; -import { useCustomCompareEffect } from '../..'; +import { useCustomCompareEffect, useUpdateEffect } from '../..'; + +/** + * A little hash function to generate a random number used to identify a string. + * @see https://stackoverflow.com/a/52171480/7304377 + */ +const hashWithCYRB53 = (someString: string) => { + let h1 = 0xde_ad_be_ef; + let h2 = 0x41_c6_ce_57; + + /* eslint-disable no-bitwise */ + for (let i = 0, ch; i < someString.length; i++) { + ch = someString.codePointAt(i) ?? 0; + h1 = Math.imul(h1 ^ ch, 2_654_435_761); + h2 = Math.imul(h2 ^ ch, 1_597_334_677); + } + h1 = Math.imul(h1 ^ (h1 >>> 16), 2_246_822_507) ^ Math.imul(h2 ^ (h2 >>> 13), 3_266_489_909); + h2 = Math.imul(h2 ^ (h2 >>> 16), 2_246_822_507) ^ Math.imul(h1 ^ (h1 >>> 13), 3_266_489_909); + + return 4_294_967_296 * (2_097_151 & h2) + (h1 >>> 0); + /* eslint-enable no-bitwise */ +}; export const Example: React.FC = () => { - useCustomCompareEffect(() => {}, []); + const [valueA, setValueA] = React.useState(0); + const [valueB, setValueB] = React.useState(0); + const [irrelevantValue, setIrrelevantValue] = React.useState(0); + + const incrementValueA = () => setValueA((prev) => prev + 1); + const incrementValueB = () => setValueB((prev) => prev + 1); + const incrementIrrelevantValue = () => setIrrelevantValue((prev) => prev + 1); + + const objectA = { key: valueA }; + const objectB = { key: valueB }; + + useCustomCompareEffect( + () => { + // eslint-disable-next-line no-alert + window.alert('Detected checksum difference from previous render!'); + }, + [objectA, objectB], + (a, b) => hashWithCYRB53(JSON.stringify(a)) === hashWithCYRB53(JSON.stringify(b)), + useUpdateEffect + ); return ( -
+ In this example, there exist two objects in memory that are initialized identically. There + is an alert that only appears when the objects differ. You can press either button to adjust + each object's only value. We hash the objects and use those hashes as a checksum to + determine if the objects have changed. This is ridiculous for the objects here (because + they're tiny and simple), but if you have potentially large, complex objects you may + wish to go this route instead of leveraging some sort of deep equality check. +
- We don't have an example for useCustomCompareEffect yet. Want to{' '} - contribute one? + We're also using `useUpdateEffect` instead of `useEffect` to avoid running the effect + on the initial mount of the component.
+ + + +