I am using inline arrow function to change the onClick
handlers of some divs in my React component, but I know it is not a good way in terms of performance.
Objectively, what is the most efficient way of setting onClick
handlers that require arguments? This is what I have tried:
1. Inline arrow function
changeRoute (routeName) {
console.log(routeName)
}
render() {
return (
<>
<div onClick={() => this.changeRoute("page1")}>1</div>
<div onClick={() => this.changeRoute("page2")}>2</div>
</>
)
}
2. If I use constructor binding then how can I pass props?
constructor() {
super(props)
this.changeRoute = this.changeRoute.bind(this)
}
changeRoute (routeName) {
console.log(routeName)
}
render() {
return (
<>
<div onClick={this.changeRoute}>1</div>
<div onClick={this.changeRoute}>2</div>
</>
)
}
3. If I remove the arrow function then the function being called on the render itself
changeRoute (routeName) {
console.log(routeName)
}
render() {
return (
<>
<div onClick={this.changeRoute("page1")}>1</div>
<div onClick={this.changeRoute("page2")}>2</div>
</>
)
}
4. If I use inline binding then it is also not best with performance
changeRoute (routeName) {
console.log(routeName)
}
render() {
return (
<>
<div onClick={this.changeRoute.bind(this, "page1")}>1</div>
<div onClick={this.changeRoute.bind(this, "page2")}>2</div>
</>
)
}
Then how can I proceed with the best way passing parameters?
To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .
Passing the event object of react as the second argument. If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.
The task is to pass a string as a parameter on onClick function using javascript, we're going to discuss few techniques. Example 1: This example simply put the argument which is string in the onClick attribute of the button which calls a function with a string as an argument using onClick() method.
When you add an onClick prop to the LinkButton component, it is just a property of an object. By calling props. onClick from inside of that component you are just calling a function that is stored inside of a property, similar to this: let props = { onClick: function () { alert("Executed!"); } }; props.
You can use arrow function to define your changeRoute
handler.
This is known as Class field syntax
. More on it here in official react docs.
constructor() {
super(props)
}
changeRoute = (parameter) => (event) => {
// business logic for route change.
}
Then you can use this function directly like so:
render() {
return (
<>
<div onClick={changeRoute(params1)}>1</div>
<div onClick={changeRoute(params2)}>2</div>
</>
)
}
You do not have to worry about the binding.
Arrow functions inherit their parent's this
.
You can add a data to your div:
<div data-id={1} onClick={this.changeRoute}>1</div>
Then you can retrieve that data in your onClick handler:
onClick = (event) => {
const id = event.currentTarget.dataset.id;
}
#1 is fine.
#2 is also 'fine', but you need to pass props, then the render function will look exactly like #1. You will be calling the bind
'd function, because you replaced it in the constructor.
#3 is just wrong, as the function gets called during render.
And regarding #4, from react docs
We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.
This causes a performance penalty when your function is used in its child components and will cause the child components to re-render (its not in your case). So you shouldn't do #4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With