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

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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