Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to poll every n seconds in react js

import React, { useState, useEffect, useRef } from 'react';

function useInterval(callback, delay) {
  const savedCallback = useRef();

  // Remember the latest callback.
  useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  // Set up the interval.
  useEffect(() => {
    function tick() {
      savedCallback.current();
    }
    if (delay !== null) {
      let id = setInterval(tick, delay);
      return () => clearInterval(id);
    }
  }, [delay]);
}

function Counter() {
  let [count, setCount] = useState(0);

  useInterval(() => {
    // Your custom logic here
    setCount(count + 1);
  }, 1000);

  return <h1>{count}</h1>;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: include nested childs 
Javascript :: c# to json online 
Javascript :: what is fn extend 
Javascript :: cara install parrot os di virtualbox 
Javascript :: Fix the transition judder at 0/60 seconds javascript30 js clock 
Javascript :: javaScript Bezier Curve After Effects expression 
Javascript :: bootstrap dropdown with state 
Javascript :: nodejs how to beautify mysql arrays 
Javascript :: Node-Red Custom UI 
Javascript :: validate vpa api razorpay stackoverflow 
Javascript :: js The equivalent of destructuring arrays and objects 
Javascript :: nodejs createwriteStream file image broken 
Javascript :: JAVASCRIPT EX. 
Javascript :: check if the last character of word is "A" 
Javascript :: Remove the minimum 
Javascript :: hacker news api react 
Javascript :: remove all elements contained in another array 
Javascript :: volta node list 
Javascript :: Example Of _.extend 
Javascript :: allow only numbers in textbox javascript onkeypress 
Javascript :: enzyme debounce test 
Javascript :: convert snake case to camelcase javascript recursive 
Javascript :: Register Multiple Models In Admin 
Javascript :: js two operations in ternary 
Javascript :: multiple populate on same level 
Javascript :: node_modules is not generated in docker 
Javascript :: events in node js 
Javascript :: access data from dofferent file in js 
Javascript :: express-roteamento 
Javascript :: counting number of times a string is in another string 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =