我是新來的反應,JS,我需要在部件的方法結合一些澄清沒有參數的道具方法並沒有得到子組件稱爲反應
我有2個部件爲父級和ChildComponent
父
var ParentComponent = React.createClass({
methodArg: function (value) {
console.log("methodArg called", value);
},
methodNoArg: function() {
console.log("methodNoArg called");
},
render: function() {
return <ChildComponent m1={(value) => this.methodArg(value)} m2={() => this.methodNoArg} />
}
})
兒童
var ChildComponent = React.createClass({
render: function() {
return (
<div>
<button onClick={()=>this.props.m1(100)}>Call M1</button>
<button onClick={()=>this.props.m2()}>Call M2</button>
</div>
)
}
})
當我點擊呼叫M1按鈕,methodArg()母公司獲取調用。
但是當我點擊致電M2methodNoArg()沒有被調用。這有什麼問題?
當我通過methodNoArg於兒童,它獲取調用
<ChildComponent m1={this.methodArg()} m2={this.methodNoArg} />
但methodArg()獲取調用沒有點擊該按鈕,它獲取調用每次當子組件呈現。
你缺少'()''後this.methodNoArg' - 'this.methodNoArg'返回指針'function',你還是要調用它 – pwolaq