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

176
Vistas
Can I call a variable created with javascript in React?

Can I call a variable whose path is in /public/assets/js in my React project within useEffect() in React views?

For example, can I use jsVal like below?

useEffect(() => {
   const existingScript = jsVal.getSum();
   ...
}

The reason why I am curious is because, when the screen made with React is turned on with chrome, jsVal is not 'jsVal is not defined' in the Console Tab of Developer Tools.

However, in useEffect, jsVal comes out as 'can not find Name', so I leave a question.

Add The location where jsVal is defined is /public/assets/js/test.js.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If your value is exposed in some way through global or window then yes, you can access it in React!

i.e.

// public/assets/file.js
window.jsVal = {
  getSum(a, b) { return a + b; }
}

// In case of typescript, add an ambient declaration too!
declare global {
  interface Window {
    jsVal: {
      getSum(a: number, b: number): number;
    }
  }
}

// React app (make sure file.js is included before React in index.html)
const MyComponent = () => {
  const [val, setVal] = useState(null);

  useEffect(
    () => {
      setVal(window.jsVal.getSum(10, 32)); // 42
    },
    [window.jsVal, setVal]
  );

  return null;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I assume jsVal is a global service/variable ? If so.

Everything of React is javascript. You can think React is a global variable.

  React.render(<App />, el)

So in general, it depends on if jsVal is defined early or later than render. However useEffect is also a bit special. It actually fires in another Javascript task/tick, similar to setTimeout or an API, so its execution is deferred in async way.

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