How to use the useEffect function from react

Find comprehensive JavaScript react.useEffect code examples handpicked from public code repositorys.

94
95
96
97
98
99
100
101
102
103
      ref.setDestinations = setDestinations;
    }
  },
});

React.useEffect(() => {
  if (destinationsProp != null) {
    setDestinations(destinationsProp);
  }
}, [setDestinations, destinationsProp]);
fork icon108
star icon594
watch icon27

474
475
476
477
478
479
480
481
482
483
    timeoutId.current = 0;
  }
  !keepHover && unsetHover();
};
useItemEffect(isDisabled, itemRef, updateItems);
react.useEffect(function () {
  return function () {
    return clearTimeout(timeoutId.current);
  };
}, []);
fork icon48
star icon881
watch icon6

+ 9 other calls in file

29
30
31
32
33
34
35
36
37
38

        return `${ props.currentComponent.name } | ${ props.opts.title }`

})()

useEffect(() => { document.documentElement.style.setProperty('--size-vertical', `${ props.size.vertical }px`) }, [ props.size.vertical ])
useEffect(() => { document.documentElement.style.setProperty('--size-horizontal', `${ props.size.horizontal }px`) }, [ props.size.horizontal ])
useEffect(() => { document.title = title }, [ title ])

// Something went wrong
fork icon23
star icon359
watch icon13

+ 5 other calls in file

179
180
181
182
183
184
185
186
187
188
189
Accordion.displayName = 'Accordion';


let current = 0;
const useIdShim = () => {
  const [id, setId] = react.useState();
  react.useEffect(() => setId(++current), []);
  return id && `${ACCORDION_PREFIX}-${id}`;
};
const _useId = react.useId || useIdShim;

fork icon4
star icon84
watch icon0

1293
1294
1295
1296
1297
1298
1299
1300
1301
1302

var _useState = React.useState({}),
    _useState2 = _slicedToArray(_useState, 1),
    _key = _useState2[0];

React.useEffect(function () {
  previousData.set(_key, {});
  initialRender.set(_key, true);
  return function () {
    previousData.delete(_key);
fork icon6
star icon9
watch icon3

+ 11 other calls in file

36
37
38
39
40
41
42
43
44
    trans = await axios.post("http://localhost:3000/api", obj);
  }
  setData(trans);
}

useEffect(() => {
  dataFun("get");
  dataPost("get");
}, []);
fork icon0
star icon1
watch icon1

+ 8 other calls in file

91
92
93
94
95
96
97
98
99
100
101
const useObservable = (paths, timeout = 100) => {
  if (!paths) return;
  paths = split(/\s*,\s*/, paths);
  const [v, s] = useState(0);
  const rerender = timeout ? throttle(timeout, () => s(v + 1)) : () => s(v + 1);
  useEffect(() => juxt(paths.map((p) => State.subscribe(p, rerender))));
  return State;
};


module.exports = {
fork icon0
star icon0
watch icon1

+ 2 other calls in file

27
28
29
30
31
32
33
34
35
36
    catch (e) {
        logger(e);
        return defaultValue;
    }
}), storedValue = _a[0], setValue = _a[1];
react_1.useEffect(function () {
    if (typeof window === "undefined")
        return;
    var updateLocalStorage = function () {
        if (storedValue !== undefined) {
fork icon0
star icon0
watch icon0

137
138
139
140
141
142
143
144
145
146
 * So if we detect a situtation where optimised appear animations
 * are running, we use useLayoutEffect to trigger animations.
 */
const useAnimateChangesEffect = window.HandoffAppearAnimations
    ? useIsomorphicLayoutEffect
    : React.useEffect;
useAnimateChangesEffect(() => {
    if (visualElement && visualElement.animationState) {
        visualElement.animationState.animateChanges();
    }
fork icon0
star icon0
watch icon1

+ 10 other calls in file

38
39
40
41
42
43
44
45
46
47
// ========================== Effect ==========================
var _React$useState = React.useState([]),
  _React$useState2 = (0, _slicedToArray2['default'])(_React$useState, 2),
  actionQueue = _React$useState2[0],
  setActionQueue = _React$useState2[1];
React.useEffect(
  function () {
    if (actionQueue.length) {
      var cloneQueue = (0, _toConsumableArray2['default'])(actionQueue);
      cloneQueue.forEach(function (action) {
fork icon0
star icon0
watch icon2