When the code gets compiled from JSX to pure JS, many props are simply object properties. For example:
<input type="text" onChange={this.onInputChange()} />
This gets compiled to:
React.createElement("input", {
type: "text",
onChange: this.onInputChange()
});
Just like in ordinary JS, if you invoke a function directly inside the prop or object, that function will be invoked when the object is evaluated for the first time.
const fn = () => {
console.log('fn running');
return 'foo';
};
const obj = {
prop: fn()
};
console.log('obj is', obj);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…