gpt4 book ai didi

react-native - react native : can't call a parent method from child

转载 作者:行者123 更新时间:2023-12-04 05:20:56 25 4
gpt4 key购买 nike

我在父组件中有这个函数,我想在子组件中调用这个方法/函数,像这样:

buttonPressed = () => {
console.log('button pressed')
}

<childComp ref={ref => this.feedback = ref}
onPress={this.buttonPressed}
/>

在 child 身上:

<Animated.View>
... // other views
<View>
<TouchableOpacity onPress={this.props.buttonPressed}> // calling here
...
</TouchableOpacity>

</View>
</Animated.View>

无论我怎么尝试,都没有用。

最佳答案

您正在将 onPress Prop 传递给 child 并在 child 中调用 buttonPressed。它应该是这样的:

<Animated.View>
... // other views
<View>
<TouchableOpacity onPress={this.props.onPress}> // calling here
...
</TouchableOpacity>

</View>
</Animated.View>

关于react-native - react native : can't call a parent method from child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47283809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com