I am new in react world, and while reading tutorial stuffs, I've noticed that using arrow-function
in JSX
causes some performance issue on re-render, and also using bind
in JSX
is forbidden due to their rendering performance impact.
Before reading that tutorial, my JSX
were like this
export class LabScreen2 extends Component {
render() {
const y = 'This is y value';
return (
<MyCustomButton onMyCustomButtonPressed={(x : string) => {
alert(x+' '+y);
}}/>
);
}
}
class MyCustomButton extends Component {
render() {
const x = 'This is x value';
return (
<Button title={'My Custom Button'} onPress={() => {
this.props.onMyCustomButtonPressed(x);
}}/>
);
}
}
and after, I've removed the arrow-functions
,
export class LabScreen2 extends Component {
render() {
...
return (
<MyCustomButton onMyCustomButtonPressed={this.onMyCustomButtonPressed}/>
);
}
onMyCustomButtonPressed = (x : string, y : string) => {
alert(x+' '+y);
}
}
class MyCustomButton extends Component {
render() {
...
return (
<Button title={'My Custom Button'} onPress={this.props.onMyCustomButtonPressed}/>
);
}
}
But now I don't know how to pass the x
and y
to onMyCustomButtonPressed
method.
I knw it'll work this way, using bind
<MyCustomButton onMyCustomButtonPressed={this.onMyCustomButtonPressed.bind(this,y)}/>
and
<Button title={'My Custom Button'} onPress={this.props.onMyCustomButtonPressed.bind(this,x)}/>
But using bind in JSX
is forbidden, right ? So, What's the perfect way to pass params in JSX
efficiently ?
Edit
I've already read this answer. It tells to pass the param as prop, and return back it using the inner method. That sounds little bit complicated. The flow would be correct, but it'll be hard to understand the working of the component as the size increases. I've also read the alternative solution mentioned, using memorize
, but that also noted as an imperfect solution. Is there any built-in perfect solution for this problem ?