React Native - 从组件中获取价值 - onPress
React Native - Get value from component - onPress
我正在尝试从已按下但无法传递的组件中获取值。
我的 Welcome class 正在使用 Menu 组件,而此组件有 4 个 MenuItem 组件。
我的问题在哪里?我无法将 itemOnPress 值输入欢迎屏幕。
欢迎光临:
class Welcome extends React.Component {
menuItemOnPress = (item) => {
console.log('ITEM', item);
};
render() {
return (
<Modal animationType={'fade'}
transparent={true}
visible={this.state.modalVisible}>
<View style={styles.modal}>
<Menu itemOnPress={(item) => this.menuItemOnPress(item)}/>
</View>
</Modal>
);
}
菜单:
class Menu extends React.Component {
render() {
const {closeOnPress, itemOnPress} = this.props;
return (
<SafeAreaView style={styles.container}>
<MenuItem titleText={'XYZ'} onPress={itemOnPress(0)}/>
<MenuItem titleText={'XYZ'} onPress={itemOnPress(1)}/>
</SafeAreaView>
);
}
将菜单组件的 onPress 功能切换为箭头功能
class Menu extends React.Component {
render() {
const {closeOnPress, itemOnPress} = this.props;
return (
<SafeAreaView style={styles.container}>
<MenuItem titleText={'XYZ'} onPress={() => itemOnPress(0)}/>
<MenuItem titleText={'XYZ'} onPress={() => itemOnPress(1)}/>
</SafeAreaView>
);
}
我正在尝试从已按下但无法传递的组件中获取值。
我的 Welcome class 正在使用 Menu 组件,而此组件有 4 个 MenuItem 组件。
我的问题在哪里?我无法将 itemOnPress 值输入欢迎屏幕。
欢迎光临:
class Welcome extends React.Component {
menuItemOnPress = (item) => {
console.log('ITEM', item);
};
render() {
return (
<Modal animationType={'fade'}
transparent={true}
visible={this.state.modalVisible}>
<View style={styles.modal}>
<Menu itemOnPress={(item) => this.menuItemOnPress(item)}/>
</View>
</Modal>
);
}
菜单:
class Menu extends React.Component {
render() {
const {closeOnPress, itemOnPress} = this.props;
return (
<SafeAreaView style={styles.container}>
<MenuItem titleText={'XYZ'} onPress={itemOnPress(0)}/>
<MenuItem titleText={'XYZ'} onPress={itemOnPress(1)}/>
</SafeAreaView>
);
}
将菜单组件的 onPress 功能切换为箭头功能
class Menu extends React.Component {
render() {
const {closeOnPress, itemOnPress} = this.props;
return (
<SafeAreaView style={styles.container}>
<MenuItem titleText={'XYZ'} onPress={() => itemOnPress(0)}/>
<MenuItem titleText={'XYZ'} onPress={() => itemOnPress(1)}/>
</SafeAreaView>
);
}