Skip to content

Commit

Permalink
docs(Add example for useDeepCompareEffect): Added missing example for…
Browse files Browse the repository at this point in the history
… useDeepCompareEffect (#964)

Added missing example for useDeepCompareEffect.

fix #582
  • Loading branch information
ArttuOll authored and xobotyi committed Oct 22, 2022
1 parent d93d0e7 commit 4189dd6
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/useDeepCompareEffect/__docs__/example.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
/* eslint-disable no-console */
import * as React from 'react';
import { useDeepCompareEffect } from '../..';
import { useEffect } from 'react';
import { useDeepCompareEffect } from '../useDeepCompareEffect';
import { useRerender } from '../../useRerender/useRerender';

export const Example: React.FC = () => {
useDeepCompareEffect(() => {}, []);
const rerender = useRerender();

// eslint-disable-next-line react-hooks/exhaustive-deps
const newOnEveryRender = {
name: 'Foo',
};

useEffect(() => {
console.log('I do get logged on every render.');
}, [newOnEveryRender]);

useDeepCompareEffect(() => {
console.log('I do not get logged on every render.');
}, [newOnEveryRender]);

return (
<div>
<>
<p>Open you browser console and the code for this example.</p>
<p>
We don&apos;t have an example for useDeepCompareEffect yet. Want to{' '}
<a href="https://github.com/react-hookz/web/issues/582">contribute one</a>?
Repeatedly press the button below. Notice, how the useEffect gets run on every render, but
useDeepCompareEffect does not. This is because useDeepCompareEffect determines dependency
changes by deep comparison instead of by reference like useEffect.
</p>
</div>
<button onClick={rerender}>Rerender</button>
</>
);
};

0 comments on commit 4189dd6

Please sign in to comment.