Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

47
Vistas
Is it a good practice to use React useEffect as listener to trigger action?

I have a ProgressBar component to get the progress I subscribe to two different events because the user uploads two different files. Each event returns the upload progress of each file and then I combine these two progress into a single one and display it to the user.

The question is: I have an useEffect that listen to these two other progress and when they're updated I update the "final" progress by summing the values or mutiplying by 2 if there is only a single source of progress (because the user can upload only one file). Is it okay to use useEffect like this?

Component code:

const ProgressBar = () => {
const [progressVideo, setProgressVideo] = useState(0);
const [progressData, setProgressData] = useState(0);
const [progress, setProgress] = useState(0);

useEffect(() => {
  window.electron.api.globals.ipcRenderer.on(
    "recorder:source1",
    (evt, message) => {
    console.log("message1 ", message.percent);
    setProgressVideo(message?.percent / 2);
  }
);
  window.electron.api.globals.ipcRenderer.on(
  "recorder:source2",
  (evt, message) => {
    console.log("message2 ", message.percent);
    setProgressData(message?.percent / 2);
  }
);
}, []);

useEffect(() => {
if (isNaN(progressVideo) && isNaN(progressData)) return;

const isSingleSourceVideo = isNaN(progressData);
if (isSingleSourceVideo) {
  setProgress(progressVideo * 2);
} else {
  setProgress(progressVideo + progressData);
}
}, [progressVideo, progressData]);

return (
  <div id={styles.progressBarWrapper}>
    <div id={styles.progressBarBackground}>
      <p>{parseInt(progress)}%</p>
    </div>
    <div
      id={styles.progressBar}
      style={{ width: parseInt(progress) + "%" }}
    ></div>
  </div>
 );
};
7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's good as long as you do clean up and remove the listeners when they are done

useEffect(() => {
  
  function calculate(evt, message){
      console.log("Message", message.percent);
    setProgressVideo(message?.percent / 2);
  }

  window.electron.api.globals.ipcRenderer.on(
    "recorder:source1", calculate
);
  window.electron.api.globals.ipcRenderer.on(
  "recorder:source2", calculate
);

return () => {
  window.electron.api.globals.ipcRenderer.removeListener(
    "recorder:source1", calculate
);
  window.electron.api.globals.ipcRenderer.removeListener(
  "recorder:source2", calculate
);
}
}, []);

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.