I am try to implement event handler function to avoid a new function creation every time the component renders
and re-renders
.
Scenario1:
If I bind the function in constructor like below and without parameters in onChange
it will create a new function in bundle file only once
constructor(props){
super(props);
this.handleChange = this.handleChange.bind(this);
this.state = {
value: ""
}
}
handleChange(e){
this.setState({
value: e.target.value
});
}
render(){
const { value } = this.state;
return(
<div>
<input type="text" onChange={this.handleChange} value={value} />
</div>
)
}
Scenario2:
But when I want to pass some parameters
along with event
to handleChange
function like below I believe it will create a new function every time the component renders
and re-renders
constructor(props){
super(props);
this.handleChange = this.handleChange.bind(this);
this.state = {
value: ""
}
}
handleChange(e, param1, param2){
this.setState({
value: e.target.value
});
}
render(){
const { value } = this.state;
return(
<div>
<input type="text" onChange={e => this.handleChange(e, param1, param2)} value={value} />
</div>
)
}
So,
how can I write scenario 2 better so that a new function will be created only once in bundle file but not every time the component renders and re-renders? Is it possible?
Edit:
param1 and param2 are my own custom values.