File size: 996 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useCounter, useCustomCompareEffect } from '../src';
import ShowDocs from './util/ShowDocs';
import isDeepEqual from '../src/misc/isDeepEqual';

const Demo = () => {
  const [countNormal, { inc: incNormal }] = useCounter(0);
  const [countDeep, { inc: incDeep }] = useCounter(0);
  const options = { max: 500 };

  React.useEffect(() => {
    if (countNormal < options.max) {
      incNormal();
    }
  }, [options]);

  useCustomCompareEffect(
    () => {
      if (countNormal < options.max) {
        incDeep();
      }
    },
    [options],
    (prevDeps, nextDeps) => isDeepEqual(prevDeps, nextDeps)
  );

  return (
    <div>
      <p>useEffect: {countNormal}</p>
      <p>useCustomCompareEffect: {countDeep}</p>
    </div>
  );
};

storiesOf('Lifecycle/useCustomCompareEffect', module)
  .add('Docs', () => <ShowDocs md={require('../docs/useCustomCompareEffect.md')} />)
  .add('Demo', () => <Demo />);