从函数中的子组件调用 prop 不起作用



我想将道具从子组件传递到父组件(确切地说,当在子组件中完成某些工作时,在父组件中调用函数(。 但它不能像我想要的那样工作!

这是子组合的一些代码:

handle = () =>
{
return this.props.DoLoggin;
}
render(){
return(
<Button  onPress={this.handle}><Text>Click</Text></Button>
);
}

和家长 :

handler = ()=> {
console.log('Logged In :)');
}
render(){
return (
<Login DoLoggin={this.handler}/>
);
}

注意:当我在像onPress这样的方法中使用调用道具时,它有效!

render(){
return(
<Button onPress={this.props.DoLoggin}><Text>Click</Text></Button>
);
}

但这不是我想要的! 我想调用 prop,在一个函数中自动做一些工作!

它直接工作的原因是因为你没有调用函数本身。它只是对用户按下按钮时将调用的函数的引用。由于您使用的是自定义处理程序,因此必须在return语句中调用该函数

您必须在handle函数中调用 prop 函数。

handle = () =>
{
return this.props.DoLoggin();
}

你必须称它为:

handle = () =>
{
return this.props.DoLoggin(); //not this.props.DoLoggin;
}

最新更新