Javascript 在onPress React Native中传递道具和功能
我正在调用文件中的一个组件,因此我在父组件中创建了一个函数,并希望点击子组件的函数Javascript 在onPress React Native中传递道具和功能,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在调用文件中的一个组件,因此我在父组件中创建了一个函数,并希望点击子组件的函数onPress,同时点击子组件中的hideMenu函数。请给我一个解决方案。谢谢 父组件 onView(){ alert('Dairu'); } {this.state.clientsList.map((item) => { return ( <View style={styles.caseItem} key={item.ID}> <C
onPress
,同时点击子组件中的hideMenu
函数。请给我一个解决方案。谢谢
父组件
onView(){
alert('Dairu');
}
{this.state.clientsList.map((item) => {
return (
<View style={styles.caseItem} key={item.ID}>
<Card style={styles.card}>
<CardItem>
<Body>
<View style={styles.rowTitle}>
<Text style={styles.title}>{item.FullName}</Text>
<CustomMenu onView={() => this.onView()} />
</View>
<View>
<Text style={styles.lbl}>Email: <Text style={styles.lblValue}>{item.EmailID}</Text></Text>
<Text style={styles.lbl}>Client Type: <Text style={styles.lblValue}>{item.ClientType}</Text></Text>
</View>
</Body>
</CardItem>
</Card>
</View>
);
})}
onView(){
警惕(“大儒”);
}
{this.state.clientsList.map((项)=>{
返回(
{item.FullName}
this.onView()}/>
电子邮件:{item.EmailID}
客户端类型:{item.ClientType}
);
})}
子组件
hideMenu = () => {
this._menu.hide();
};
render() {
return (
<Menu
ref={this.setMenuRef}
button={<Icon type="Feather" name="more-vertical" onPress={this.showMenu} style={{ fontSize: 20, color: '#555' }} />}
>
<MenuItem onPress={this.props.onView}>View</MenuItem>
<MenuItem onPress={this.hideMenu}>Edit</MenuItem>
<MenuItem onPress={this.hideMenu}>Delete </MenuItem>
</Menu>
);
}
hideMenu=()=>{
这个._menu.hide();
};
render(){
返回(
看法
编辑
删去
);
}
您已将onView道具传递给子对象。只需在子组件的hideMenu方法中调用此方法:
hideMenu = () => {
this._menu.hide();
this.props.onView();
};
onView={(arg1, arg2)=> onView(arg1, arg2)}
您只需在hideMenu函数中添加
this.props.onView()
hideMenu = () => {
this._menu.hide();
this.props.onView();
};
此外,还可以将参数传递给父组件,如:
this.props.onView(arg1, arg2);
在父组件中:
hideMenu = () => {
this._menu.hide();
this.props.onView();
};
onView={(arg1, arg2)=> onView(arg1, arg2)}
使用react web,如果您的子组件是
CustomMenu
,则可以调用'hideMenu=()=>{this.\u menu.hide();this.props.onView()}@mpc没有收到您的point@ZaInKhAn你能再告诉我一点你想做什么吗?@VahidAkhtar我想删除子组件MenuItem
的记录onPress
,因此为此我在父组件中创建了一个函数,它可以工作,但我还需要在同一个子组件上点击hideMenu
函数on按
。