S
S
sinneren2018-11-06 14:37:03
React
sinneren, 2018-11-06 14:37:03

Why does the onBlur event in react-contenteditable always fire when an argument is passed to the method?

I'm using react-contenteditable.
It has an onBlur prop in which I wrote the handleChange method. If I pass the method without an argument , then everything is OK, but as soon as I change it to that is, any static argument, the onBlur event starts executing upon loading, with every click on almost any place, in general, it is hung on every element. onBlur={this.handleChange}onBlur={this.handleChange('title')}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-11-06
@sinneren

passing a method without an argumentonBlur={this.handleChange}

T-ah-ah-ah-k, clearly. Do you realize that in the first case, you are assigning the handleChange method as the onBlur handler, and in the second case, the result of its execution? Do you even understand that a function and a function call are not the same thing?
If you want to pass a parameter, do this: . And better - instead of passing the value as a parameter, add it to the element as an attribute, and read in the event handler:onBlur={() => this.handleChange('title')}
handleChange = (e) => {
  const changeParam = e.target.getAttribute('data-change-param');
  ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question