main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
996 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import { useEffect, useRef } from 'react';
|
---|
| 2 | var isPrimitive = function (val) { return val !== Object(val); };
|
---|
| 3 | var useCustomCompareEffect = function (effect, deps, depsEqual) {
|
---|
| 4 | if (process.env.NODE_ENV !== 'production') {
|
---|
| 5 | if (!(deps instanceof Array) || !deps.length) {
|
---|
| 6 | console.warn('`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
|
---|
| 7 | }
|
---|
| 8 | if (deps.every(isPrimitive)) {
|
---|
| 9 | console.warn('`useCustomCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.');
|
---|
| 10 | }
|
---|
| 11 | if (typeof depsEqual !== 'function') {
|
---|
| 12 | console.warn('`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list');
|
---|
| 13 | }
|
---|
| 14 | }
|
---|
| 15 | var ref = useRef(undefined);
|
---|
| 16 | if (!ref.current || !depsEqual(deps, ref.current)) {
|
---|
| 17 | ref.current = deps;
|
---|
| 18 | }
|
---|
| 19 | useEffect(effect, ref.current);
|
---|
| 20 | };
|
---|
| 21 | export default useCustomCompareEffect;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.