As far as i know, there are two main pattern of calling inherited method on ReactJS, but I dont know which one should I use, or the best practice,
class ParentCont extends React.Component {
constructor(props) {
super(props);
this.state = {
myState : "My State"
}
}
callMe() {
console.log(this.state.myState);
}
render() {
return(
<div>
<MyButton myProp={this.callMe.bind(this)} />
</div>
)
}
}
class MyButton extends React.Component {
buttonB() {
this.props.myProp();
}
render() {
return(
<div>
<button onClick={this.props.myProp}>Click Me A</button>
<button onClick={this.buttonB.bind(this)}>Click Me B</button>
</div>
)
}
}
ReactDOM.render(<ParentCont />, document.getElementById('app'));
On above snippet there are two ways of calling the callMe()
on ParentCont
from MyButton
,
First, attaching the props
directly to event handler, i.e. Click Me A
button,
Second, attaching so called local function
to event handler, i.e. Click Me B
button,
Which one is the best? or what are the advantages and drawbacks of one another?