How to make a sound progress bar
Unanswered
subscr posted this in #help-forum
subscrOP
hello guys, i have a hook, which returns a calculation of the music tempo for a component, but it is returning 49 and 50 consecutively, without stopping, does anyone know?
47 Replies
hook:
import { useState, useEffect } from "react";
interface ProgressProps {
start: number,
end: number,
}
const INTERVAL_STEP = 1000;
export default function useProgress({
start,
end,
}: ProgressProps) {
const startedAt = Date.now();
const [curPosition, setCurPosition] = useState(end - startedAt);
useEffect(() => {
const interval = setInterval(() => {
setCurPosition((prevProsition) => prevProsition - INTERVAL_STEP)
}, INTERVAL_STEP)
return () => {
clearInterval(interval);
}
}, [start, end, startedAt])
useEffect(() => {
setCurPosition(end - startedAt);
}, [start, end, startedAt])
const total = end - start;
return {
percentage: 100 - Math.floor((curPosition / total) * 100),
startedAt
}
}Madeiran sardinella
Hi, I'd use a ref instead of a state in this case
@Madeiran sardinella Hi, I'd use a ref instead of a state in this case
subscrOP
just like I didn't understand
const [curPosition, setCurPosition] = useState(end - startedAt);?
@subscr just like I didn't understand
Madeiran sardinella
I mean useRef instead of useState.
subscrOP
oh, ok
Madeiran sardinella
I think you don't need the second useEffect, what are you using it for?
subscrOP
to set the current song position
Madeiran sardinella
Ok, I understand, can you try removing startedAt from array dependencies?
In both useEffect
I just don't understand useRef
@subscr I just don't understand useRef
Madeiran sardinella
It returns an object with a prop named current, that prop contains the value you assign. Using ref instead of state you avoid the componen re render every 1000 milliseconds
subscrOP
hmmm
@subscr ??
subscrOP
how cani apply this in my code?
Madeiran sardinella
Replace [curPos..., setCur...] for a simple variable, eg. curPosition
subscrOP
hmm
Madeiran sardinella
useRef is mutable, you can assign a value directly, using refName.current = value, instead of using the state setter
subscrOP
curPosition.current = prevPosition - INTERNAL_STEP?
Madeiran sardinella
curPosition.current = curPosition.current + INTERNAL_STEP
subscrOP
hm
@Madeiran sardinella curPosition.current = curPosition.current + INTERNAL_STEP
subscrOP
now it is only returning 50
Madeiran sardinella
Ok
Madeiran sardinella
I can help you tomorrow, is late for me
subscrOP
alr
Madeiran sardinella
Hi, hope this is helpful to you
https://stackblitz.com/edit/vitejs-vite-yj59e8?file=src%2FApp.tsx,src%2FuseProgress.tsx&terminal=dev
https://stackblitz.com/edit/vitejs-vite-yj59e8?file=src%2FApp.tsx,src%2FuseProgress.tsx&terminal=dev
Try to avoid using date to generate numbers couse it calculates a new value every time component renders
Madeiran sardinella
Change the type in the useRef declaration
It didn't worked for me
i need this for timestamps
basically, I wanted to make it so that it would receive data from a song (start as timestamp and end as timestamp)
Madeiran sardinella
It should work well with timestamps, couse are just numbers. What you can't do is generate the timestamp in the same component
@Madeiran sardinella It should work well with timestamps, couse are just numbers. What you can't do is generate the timestamp in the same component
subscrOP
I tried putting timestamps, but it didn't work
Madeiran sardinella
Let me see
Madeiran sardinella
Can you show me how you get the timestamps?
subscrOP
Date.now() + 6e5
@subscr Date.now() + 6e5
Madeiran sardinella
That's the problem, the method now of Date calculates a new timestamps every time the component is rendered
You could maybe try to memorize it
To avoid calculate it in every render