I have seen something weird when working with onChange in React.
The basic flow is to listen for changes of this.props.tableBody and trigger autoscroll to the table bottom. The tableBody is a list of items.
I use react class component and have bound the function already.
class ProgressTable extends Component {
constructor(props) {
super(props);
this.onTableChangeHandler = this.onTableChangeHandler.bind(this);
}
onTableChangeHandler() {
console.log("hi from onTableChangeHandler");
}
render() {
return (
<div className="table-body-container" onChange={this.onTableChangeHandler}> // not work
{this.props.tableBody}
</div>
);
}
}
As usual, the code should be fine.
But something weird happened here. The onChange, which should invoke onTableChangeHandler function now doesn't.
I have tried to figure out what is happening here and found that if I add () to the onChange attached function like this, the code is working and listen for the changes correctly except for the first redundant call when component render.
<div className="table-body-container" onChange={this.onTableChangeHandler()}> // work like usual
{this.props.tableBody}
</div>
Furthermore, I have tried this and it also works.
<div className="table-body-container" onChange={console.log("hi")}> // "hi"
{this.props.tableBody}
</div>
Can you show me what and why this happened?