Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

reactjs - Error "Failed to Compile" Variable is not defined no-undef

Need help with this one

I'm trying to pass an argument called product to a function which is called for onClick but I'm getting an error

Code:

import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0
  };

  handleIncrement = product => {
    console.log(product);
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <span className={this.getBagdeClasses()}>{this.formatCount()}</span>
        <button
          onClick={() => this.handleIncrement(product)}
          className="btn btn-secondary btn-sm"
        >
          Increment
        </button>
      </div>
    );
  }

  getBagdeClasses() {
    let classes = "badge m-2 badge-";
    classes += this.state.count === 0 ? "warning" : "primary";
    return classes;
  }

  formatCount() {
    const { count } = this.state;
    return count === 0 ? "Zero" : count;
  }
}

export default Counter;

Error:

Failed to compile
src/components/counter.jsx
  Line 18:47:  'product' is not defined  no-undef

Search for the keywords to learn more about each error.
This error occurred during the build time and cannot be dismissed.

I am searching for a fix but nothing seems to be working. I am a newbie to react and don't know what went wrong.

question from:https://stackoverflow.com/questions/65884509/error-failed-to-compile-variable-is-not-defined-no-undef

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just remove the Product variable from your code and it will work

import React, { Component } from "react";

class Counter extends Component {
state = {
count: 0
};

handleIncrement = ()=> this.setState({ count: this.state.count + 1 })

 render() {
 return (
  <div>
    <span className={this.getBagdeClasses()}>{this.formatCount()}</span>
    <button
      onClick={() => this.handleIncrement()}
      className="btn btn-secondary btn-sm"
    >
      Increment
    </button>
  </div>
);
 }

 getBagdeClasses() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return classes;
}

formatCount() {
 const { count } = this.state;
return count === 0 ? "Zero" : count;
}
 }

export default Counter;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...