最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - console.log not working in arrow function - Stack Overflow

matteradmin6PV0评论

I have declared a arrow function inside react ponent. inside this i have logged a value but it shows error. I am new in react so please help me out.

Where I have declared the function

class DeliveryPage extends Component{
   onChange : (event) => { console.log('change', event.target.value); }
}

I have invoked the function here

  <ComboBox
    data={source}
    onChange={this.onChange}
    onFilterChange={this.onFilterChange}
    filterable={true}
  />

Then i have changed my code to

    onChange = (event) => { console.log('change', event.target.value); }

I have declared a arrow function inside react ponent. inside this i have logged a value but it shows error. I am new in react so please help me out.

Where I have declared the function

class DeliveryPage extends Component{
   onChange : (event) => { console.log('change', event.target.value); }
}

I have invoked the function here

  <ComboBox
    data={source}
    onChange={this.onChange}
    onFilterChange={this.onFilterChange}
    filterable={true}
  />

Then i have changed my code to

    onChange = (event) => { console.log('change', event.target.value); }

Share Improve this question edited May 5, 2019 at 7:43 Tanveer Hasan asked May 5, 2019 at 7:34 Tanveer HasanTanveer Hasan 3431 gold badge5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
onChange : (event) => { console.log('change', event.target.value); }

This is incorrect syntax, it should be:

onChange = (event) => { console.log('change', event.target.value); }

If the Arrow Functions don't work, then you most likely don't have the proposal-class-properties functionally, which can be installed via the following Babel Plugin: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties.

Otherwise, try this method similar to this:

class DeliveryPage extends Component{
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);
  }

  onChange(event) {
    console.log('change', event.target.value);
  }
}

You should be trying this

class DeliveryPage extends Component{
   onChange = (event) => {
     console.log('change', event.target.value);
  }
}
Post a comment

comment list (0)

  1. No comments so far