Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
Do I need to keep those variables global?

Once I reached out of scopes, from events, I need to keep referencing variables that was declared in the parent scope. In below code, fitAddon. In order to prevent JS's GC to clean up my variable, do I need to keep it global like below? this isn't much clear to me.

// do I need to keep this to use in handleResize() event or
// it would be fine if it was local, i.e., same scope as
// it was initialized?
 let fitAddon: FitAddon;

export const Terminal = () => {

  const id = 'xterm-container';
  useEffect(() => {
    const terminal = new TerminalType({
      cursorBlink: true,
      cursorStyle: window.api.isWindows ? "bar" : "underline",
      cols: DefaultTerminalSize.cols,
      rows: DefaultTerminalSize.rows,
    });

    fitAddon = new FitAddon();
    terminal.loadAddon(fitAddon);
    
    terminal.open(document.getElementById(id) as HTMLElement);
    fitAddon.fit();
    

    function handleResize() {
      fitAddon.fit();
    }

    window.addEventListener('resize', handleResize);

    return () => {
      return window.removeEventListener('resize', handleResize);
    }
  }, []);

  return (
    <div id={id}></div>
  )
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

No, you don't.

As long as the listener exists:

window.addEventListener('resize', handleResize);

the callback will not be garbage collected. Since the callback references fitAddon, fitAddon will not be garbage collected either, no matter where it's declared. So, feel free to declare it inside the effect callback.

const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
// ...
about 4 years 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 vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda