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

158
Visualizações
My variable is undefined in my render method

I have a variable that I need in my React class.

But when I define it in the construtor, it becomes undefined when I try to use it in the render() method.

How can I make it so that my variable is available in the render() method?

Here is what I have:

export default class GameConsole extends React.PureComponent {

constructor(props) {
    super(props);

    var domainUrl = window.location.origin;
}

render() {
    return (
        <div>
            <GameControl
               init={{
                  masterUrl = domainUrl;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Write this:

export default class GameConsole extends React.PureComponent {

constructor(props) {
    super(props);
        
}


render() {

var domainUrl = window.location.origin;
    return (
        <div>
            <GameControl
               init={{
                  masterUrl = domainUrl;
about 4 years ago · Juan Pablo Isaza Relatório

0

Assign the value to the component's local state:

constructor() {
  super();
  this.state = {
    domainUrl: window.location.origin,
  };
}

render() {
  // Can access it on `this.state` inside render
  this.state.domainUrl;
}
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