Reactjs 如何在没有参数的情况下从子属性调用父函数
我不是在寻找,因为我不会在任何arg通过 我需要调用父函数,而不传入任何参数 家长(es6):Reactjs 如何在没有参数的情况下从子属性调用父函数,reactjs,Reactjs,我不是在寻找,因为我不会在任何arg通过 我需要调用父函数,而不传入任何参数 家长(es6): foo(){ ... console.log("i was called"); } <Child callFoo={this.foo} /> // otherFunc() will be called by an onClick button event otherFunc(){ ... this.props.callFoo; } foo(bool){ ...
foo(){
...
console.log("i was called");
}
<Child callFoo={this.foo} />
// otherFunc() will be called by an onClick button event
otherFunc(){
...
this.props.callFoo;
}
foo(bool){
...
if (bool){
console.log("i was called");
}
}
<Child callFoo={this.foo} />
// otherFunc() will be called by an onClick button event
otherFunc(){
...
this.props.callFoo(true);
}
上述方法不起作用。这是可行的,但我不需要这样做:
家长(es6):
foo(){
...
console.log("i was called");
}
<Child callFoo={this.foo} />
// otherFunc() will be called by an onClick button event
otherFunc(){
...
this.props.callFoo;
}
foo(bool){
...
if (bool){
console.log("i was called");
}
}
<Child callFoo={this.foo} />
// otherFunc() will be called by an onClick button event
otherFunc(){
...
this.props.callFoo(true);
}
是否有一种方法可以通过
this.props.callFoo
简单调用函数?您不需要传递布尔值,但必须使用括号来执行callFoo
函数。以下是您的示例:
类父级扩展React.Component{
foo(){
log(“我被调用”);
}
render(){
返回(
);
}
}
子类扩展了React.Component{
otherFunc(){
this.props.callFoo();
}
render(){
返回(
小孩
);
}
}
React.render(,document.getElementById('View'))代码>
您不需要传递布尔值,但必须使用括号来执行callFoo
函数。以下是您的示例:
类父级扩展React.Component{
foo(){
log(“我被调用”);
}
render(){
返回(
);
}
}
子类扩展了React.Component{
otherFunc(){
this.props.callFoo();
}
render(){
返回(
小孩
);
}
}
React.render(,document.getElementById('View'))代码>
this.props.callFoo();这是在没有任何参数的情况下调用函数this.props.callFoo();这是在没有任何参数的情况下调用函数