Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
storing returned object from useRef in redux state

i have a question. im creating a music platform(like spotify) with next js and to manage states, i use redux toolkit. to play a music, i added audio in a component that has some components to change the music time or volume or etc. to change the volume or time or etc, i used useRef hook and i change the music time almost like this(this is a component that return some elements for large devices only and if the device is small, audio style will be display: none;):

//inside a component(for example AudioLarge.jsx)
const audioRef = useRef()

useEffect(() => {
   //if ref is ready, change the time
   if(audioRef.current) audioRef.current.currentTime = 50;
}, [])

return (
<>
   <audio src={source} ref={audioRef} />
</>
)

the problem starts here. i want to access the ref from AudioLarge.jsx in AudioSmall.jsx component that returns some elements for small devices. i have a solution for this. i can store the music ref in redux state. but is it ok? is it a good solution for this? does it make problems in future? is there a better solution to access the music ref globally? thanks for helping :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes you can store it in redux store... you can store it in any state management system from context api to redux

but the best way is to declare reference to it in a reducer and means to updated its state right thru action right there in that reducer... i.e its own reducer to keep your code neat

//in your page

import { useRef } from 'react';
.......
const audioref = useRef(null);
<audio ref = {audioref} />

somewhere in your codes you store the reference in your store thru action

<button onClick = {() => addAudioRef(audioref)}>play</button>

in your reducer

switch(state.action) {
   ....

   case 'audioref-added':
      return {...state, audioref : payload.audioref.current.play()}
   case 'audioref-stoped':
      return {...state, audioref : state.audioref.current.stop()}
   ....
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda