-1

I am new to JS and React and the error I faced is

"logic is not defined".

class Keyboard extends React.Component {
  logic() {
    return false;
  }

  render() {
    return (
      <div>
        <h1>{logic() ? "Yeah" : "Nooo"}</h1>
      </div>
    );
  }
}

const box = document.querySelector(".mir");

ReactDOM.render(<Keyboard />, box);
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Mirzhal
  • 19
  • 3

3 Answers3

1

You should use instance method using this,

<h1>{this.logic() ? "Yeah" : "Nooo"}</h1>

If you are calling another instance method in logic function, or you need to set state in logic function, in that case only you need to bind this to function in constructor,

constructor(props){
   super(props);
   this.logic = this.logic.bind(this);
}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
0

You should put the "this" keyword before any methods that is declared inside the class component

 <h1>{this.logic() ? "Yeah" : "Nooo"}</h1>
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
-1

Bind functions in the constrcutor and access it using this.

class Keyboard extends React.Component {
  constructor(props){
    this.logic = this.logic.bind(this);
  }
  logic() {
    return false;
  }

  render() {
    return (
      <div>
        <h1>{this.logic() ? "Yeah" : "Nooo"}</h1>
      </div>
    );
  }
}

const box = document.querySelector(".mir");

ReactDOM.render(<Keyboard />, box);
fiveelements
  • 3,649
  • 1
  • 17
  • 16