最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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);
  }
}
发布评论

评论列表(0)

  1. 暂无评论