Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!