Next.js Discord

Discord Forum

How to make a sound progress bar

Unanswered
subscr posted this in #help-forum
Open in Discord
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

page.tsx
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
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.
oh, ok
Madeiran sardinella
I think you don't need the second useEffect, what are you using it for?
to set the current song position
Madeiran sardinella
Ok, I understand, can you try removing startedAt from array dependencies?
In both useEffect
@Madeiran sardinella I mean useRef instead of useState.
??
idky
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
hmmm
@subscr ??
how cani apply this in my code?
Madeiran sardinella
Replace [curPos..., setCur...] for a simple variable, eg. curPosition
hmm
Madeiran sardinella
useRef is mutable, you can assign a value directly, using refName.current = value, instead of using the state setter
curPosition.current = prevPosition - INTERNAL_STEP?
Madeiran sardinella
curPosition.current = curPosition.current + INTERNAL_STEP
hm
Madeiran sardinella
Ok
the page
Madeiran sardinella
I can help you tomorrow, is late for me
alr
Madeiran sardinella
Try to avoid using date to generate numbers couse it calculates a new value every time component renders
timestamp works here?
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
Let me see
Madeiran sardinella
Can you show me how you get the timestamps?
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