父组件页面:
<CommonBottomBtn /> //子组件名称为CommonBottomBtn
在父组件中,我们想要给CommonBottomBtn这个组件进行事件的绑定,如果单纯使用onPress是不生效的。
需要在子组件中给组件进行onPress的绑定,再在父组件中调用,如下所示
父组件页面改变
<CommonBottomBtn itemClick={()=>{this.clickEvent()}} text={'传递text'}/>
子组件页面改变
<TouchableOpacity activeOpacity={0.5} onPress={()=>{this.props.itemClick()}}>
<LinearGradient style={styles.buyBtn} colors={['#6838DE','#AC61FF']}
start={
{x:0, y:0}} end={
{x:1,y:1}}>
<Text style={styles.loginBtnText}>{this.props.text}</Text>
</LinearGradient>
</TouchableOpacity>
然后再在父组件中对clickEvent进行定义,即可进行相应操作:
clickEvent(){
console.log('点击事件')
}