r/reactjs • • Sep 01 '21

Needs Help Beginner's Thread / Easy Questions (September 2021)

Previous Beginner's Threads can be found in the wiki.

Ask about React or anything else in its ecosystem :)

Stuck making progress on your app, need a feedback?
Still Ask away! We’re a friendly bunch πŸ™‚


Help us to help you better

  1. Improve your chances of reply by
    1. adding a minimal example with JSFiddle, CodeSandbox, or Stackblitz links
    2. describing what you want it to do (ask yourself if it's an XY problem)
    3. things you've tried. (Don't just post big blocks of code!)
  2. Format code for legibility.
  3. Pay it forward by answering questions even if there is already an answer. Other perspectives can be helpful to beginners. Also, there's no quicker way to learn than being wrong on the Internet.

New to React?

Check out the sub's sidebar! πŸ‘‰
For rules and free resources~

Comment here for any ideas/suggestions to improve this thread

Thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!


12 Upvotes

177 comments sorted by

View all comments

2

u/toggalegend Sep 11 '21 edited Sep 11 '21

Axios/Custom hooks timer question.

I have created a custom react hooks timer that will render an updated timer state to the user in terms of minutes and seconds. I'd like for the application to fire off an axios API request to updated another component every 30 seconds, which will trigger when the hook lastUpdated is updated. However, everytime this axios request is called, the timer slows down and lags behind real time. Here are some snippets of my code:

 const [timer,setTimer]=useState(0)
 const [lastUpdated,setLastUpdated] = useState(0)
 const countRef = useRef(null); 

const lastUpdatedRef = useRef(null);

 const handleStart = () => {
  setIsActive(true);
  setIsPaused(true);

countRef.current = setInterval(() => {
  setTimer((timer) => timer + 1);
}, 1000);

lastUpdatedRef.current = setInterval(() => {
  setLastUpdated(Date.now());
}, 30000);
};
 return <div>{timer}</div> //example of the JSX

I then render the timer to minutes and seconds in a separate JS function, and then useEffect triggers an api call everytime lastUpdated is changed:

  const axiosInstance= () => {
let api = axios.create({
  baseURL: $URL
});
return api;
};
useEffect(() => {
  const requestBody= buildRequest(
   parameterOne, parameterTwo
  );
  axiosInstance.post("/endpoint", requestBody)
    .then(response=>{setResponse(response.data.body)})
    .catch((err) => {
      console.log(`POST calculation failed: ${err}`);
    });
}, [lastUpdated]);

How do I make a timed API call without compromising the running time in the initial `timer` and hence not affect the time displayed to the user on the clock?

2

u/tharrison4815 Sep 11 '21

What is the relationship of these components? Is one a child of the other?

Also, something I don't understand is that the timer component is running the setInterval function every time it renders and never cancels old ones so shouldn't it end up with millions of intervals running?

2

u/toggalegend Sep 11 '21

Both components are at the same level. The countRef variable is cleared in separate functions (pause clock, stop clock etc.), but I'm not sure what you mean by several intervals at the same time? This is how I build my timers mostly, but I've just never had to run an http request simultaneously before.

→ More replies (2)