In react native when you have functions that need to run at render and must pass variables, most people suggest that one should use
onPress{() => this.functionName(variable)}
However when working with large lists and complex components you have to optimize your code. Creating a new function for every renderItem in a flatList
reduces performance, sometimes hugely so depending on how many functions per renderItem you are creating. So the suggestion is to move from creating a function at render to using a function reference. Like this:
functionName = () => {
//code
}
onPress={this.functionName}
However I haven't been able to figure out how to pass variables to the function with this method.
If you do this:
onPress={this.functionName(variable}
It will just run the function instantly on component load.
Any ideas?