A
A
anayks2020-11-14 11:52:00
React
anayks, 2020-11-14 11:52:00

Correct use of JS function?

Good afternoon.

I have two examples of functions in a JSX block:

First:

<div onClick={() => this.onClick(35)}>
</div>


And the second one:
<div onClick={this.onClick.bind(this, 35)}>
</div>


The context of both of these examples will be bound to the parent component.
But which of these options is more correct in principle? Reference to an anonymous function inside a component or bind?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Askhat Bikmetov, 2020-11-14
@anayks

Indeed, the code examples are equivalent (if you do not take into account that the number 35 should be the second argument to the bind function, you probably mistyped).
I will not undertake to talk about which method is better , but I myself use it more often and see arrow functions in someone else's code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question