I use TypeScript in ReactNative to access Button target.getAttribute("data-code") but getAttribute is not accessible in onPress event. How can I reference target type? I googled around the internet but neither solution helped.
import React from 'react';
import {
Alert,
Button,
GestureResponderEvent,
StyleSheet,
View,
} from 'react-native';
const App = () => {
const _onPressButton = (event: GestureResponderEvent) => {
Alert.alert(event.currentTarget.getAttribute('data-code'));
};
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button
data-code="note1"
onPress={_onPressButton}
title="Light (On)"
/>
</View>
<View style={styles.buttonContainer}>
<Button
data-code="Note2"
onPress={_onPressButton}
title="light (OFF)"
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
buttonContainer: {
margin: 20,
},
});
export default App;
3
Answers
Here is my android emulator screen shot
I tried this code:
But this happend: