React native 反应本族语:can';t从子方法调用父方法
我在父组件中有这个函数,我试图在子组件中调用这个方法/函数,如下所示:React native 反应本族语:can';t从子方法调用父方法,react-native,React Native,我在父组件中有这个函数,我试图在子组件中调用这个方法/函数,如下所示: buttonPressed = () => { console.log('button pressed') } <childComp ref={ref => this.feedback = ref} onPress={this.buttonPressed} /> 按钮按下=()=>{ console.log('按钮按下') } this.feedback=ref} onP
buttonPressed = () => {
console.log('button pressed')
}
<childComp ref={ref => this.feedback = ref}
onPress={this.buttonPressed}
/>
按钮按下=()=>{
console.log('按钮按下')
}
this.feedback=ref}
onPress={this.buttonPressed}
/>
儿童:
<Animated.View>
... // other views
<View>
<TouchableOpacity onPress={this.props.buttonPressed}> // calling here
...
</TouchableOpacity>
</View>
</Animated.View>
... // 其他观点
//在这里打电话
...
不管我怎么做,它都不起作用。您正在将Press-prop传递给child,并调用child中的按钮Pressed。应该是这样的:
... // 其他观点
//在这里打电话
...
我认为在子组件中,您应该调用this.props.onPress()
,因为您要传递的属性的名称是onPress
。另外,当您想要调用函数时,不要忘记放置()
。因此,它应该是:
<Animated.View>
... // other views
<View>
<TouchableOpacity onPress={this.props.onPress()}> // calling here
...
</TouchableOpacity>
</View>
</Animated.View>
... // 其他观点
//在这里打电话
...