mirror of
https://github.com/streamich/react-use.git
synced 2026-02-01 14:37:31 +00:00
39 lines
988 B
TypeScript
39 lines
988 B
TypeScript
import { storiesOf } from '@storybook/react';
|
|
import * as React from 'react';
|
|
import { useCounter, useCustomCompareEffect } from '../src';
|
|
import { isDeepEqual } from '../src/util';
|
|
import ShowDocs from './util/ShowDocs';
|
|
|
|
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 />);
|