I’m curious about which is considered by React Developers to be the better option (or at least more widely accepted among React Develoeprs) when using a custom button in React Native…
I’ve created a custom button component using TouchableOpacity. At first I decided to pass the function as a prop and just run it on the onPress. But I was told that I shouldn’t pass a function as props, and that a better option would be to send an object containing things I need for the function, and put the function inside the CustomButton class.
I prefer passing the function as props so that the CustomButton can be a dumb component that will run whatever function I give it. But I’ve been a React Developer for a shorter period of time than the person who told me to not to pass the function as props. However, I’ve seen “pass the function as props” as a top voted answer on StackOverflow, so I thought I’d try here to see what you all think.
Which is better:
<CustomButton onPress={()=>{this.myFunction()} />
or
<CustomButton dataForFunction={{key: value, key: value, key: value}} /> // And let the component use the object as a payload sent to a function which lives in its parent class.
For instance, in CustomButton class:
class CustomButton ... {
handleClick =(payload)=> {
...
}
render() {
return (
<TouchableOpacity onPress={ (this.props.dataForFunction) => {this.handleClick(this.props.dataForFunction)} } />
);
}
}