admin管理员组文章数量:1313161
I am newbie in React Native, I implemented onPress function on View ponent(I tried on touchableOpacity and NativeFeedBack) but it did not work on any of them. I don't understand why.
I wanted to implement buttons using images, so that when someone click on the button, the code runs. But things are not as expected.
import React, { Component } from 'react';
import {
StatusBar,
Image,
View,
Text,
Dimensions,
TouchableNativeFeedback
} from 'react-native';
import Constants from 'expo-constants';
class LandingScreen extends Component {
render() {
const resizeMode = 'cover';
const text = '';
const { width, height } = Dimensions.get('screen');
return (
<View
style={{
flex: 1,
backgroundColor: '#eee',
paddingStart:Constants.statusBarHeight
}}
>
<StatusBar hidden />
<View
style={{
position: 'absolute',
top: 0,
left: 0,
width: width,
height: height,
}}
>
<Image
style={{
flex: 1,
resizeMode,
}}
source={require('../assets/home.png') }
/>
</View>
<TouchableNativeFeedback>
<View
style={{
position: 'absolute',
bottom: 100,
left: 40,
right:50,
marginLeft:'auto',
marginRight:'auto'
}}
>
<View>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/SignInButton.png') }
/>
</View>
</View>
</TouchableNativeFeedback>
// onPress dont work here as well
<TouchableNativeFeedback >
<View
style={{
position: 'absolute',
bottom: 30,
left: 40,
right:50,
marginLeft:'auto',
marginRight:'auto'
}}
>
<View>
<Image onPress=()=>this.props.navigation.navigate('Main')}
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/LearnMoreButton.png') }
/>
</View>
</View>
</TouchableNativeFeedback>
//here the onPress dont work
<View onPress={()=>console.log("Hello")}
style={{
position: 'absolute',
bottom: 310,
left: 60,
right:60,
marginLeft:'auto',
marginRight:'auto'
}}
>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/Quote.png') }
/>
</View>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'center',
}}
>
<Text
style={{
textAlign: 'center',
fontSize: 40,
}}
>
{text}
</Text>
</View>
</View>
);
}
}
export default LandingScreen;
I am newbie in React Native, I implemented onPress function on View ponent(I tried on touchableOpacity and NativeFeedBack) but it did not work on any of them. I don't understand why.
I wanted to implement buttons using images, so that when someone click on the button, the code runs. But things are not as expected.
import React, { Component } from 'react';
import {
StatusBar,
Image,
View,
Text,
Dimensions,
TouchableNativeFeedback
} from 'react-native';
import Constants from 'expo-constants';
class LandingScreen extends Component {
render() {
const resizeMode = 'cover';
const text = '';
const { width, height } = Dimensions.get('screen');
return (
<View
style={{
flex: 1,
backgroundColor: '#eee',
paddingStart:Constants.statusBarHeight
}}
>
<StatusBar hidden />
<View
style={{
position: 'absolute',
top: 0,
left: 0,
width: width,
height: height,
}}
>
<Image
style={{
flex: 1,
resizeMode,
}}
source={require('../assets/home.png') }
/>
</View>
<TouchableNativeFeedback>
<View
style={{
position: 'absolute',
bottom: 100,
left: 40,
right:50,
marginLeft:'auto',
marginRight:'auto'
}}
>
<View>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/SignInButton.png') }
/>
</View>
</View>
</TouchableNativeFeedback>
// onPress dont work here as well
<TouchableNativeFeedback >
<View
style={{
position: 'absolute',
bottom: 30,
left: 40,
right:50,
marginLeft:'auto',
marginRight:'auto'
}}
>
<View>
<Image onPress=()=>this.props.navigation.navigate('Main')}
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/LearnMoreButton.png') }
/>
</View>
</View>
</TouchableNativeFeedback>
//here the onPress dont work
<View onPress={()=>console.log("Hello")}
style={{
position: 'absolute',
bottom: 310,
left: 60,
right:60,
marginLeft:'auto',
marginRight:'auto'
}}
>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/Quote.png') }
/>
</View>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'center',
}}
>
<Text
style={{
textAlign: 'center',
fontSize: 40,
}}
>
{text}
</Text>
</View>
</View>
);
}
}
export default LandingScreen;
Share
Improve this question
edited Aug 14, 2019 at 5:13
hong developer
13.9k4 gold badges43 silver badges73 bronze badges
asked Aug 11, 2019 at 5:03
Prateek93aPrateek93a
851 silver badge6 bronze badges
3 Answers
Reset to default 3onPress works only on Touchables it should be
<TouchableNativeFeedback onPress=()=>this.props.navigation.navigate('Main')/>
This function will not work on View or Image. You need to wrap the view inside touchable (opacity or native feedback) to get the click.
renderButton: function() {
return (
<TouchableOpacity onPress={this._onPressButton}>
<Image
style={styles.button}
source={require('./myButton.png')}
/>
</TouchableOpacity>
);
},
https://facebook.github.io/react-native/docs/touchableopacity
I've modified your code. Try this. And as @Wolverine
says, View
and images
don't have click props.
import React, { Component } from 'react';
import {
StatusBar,
Image,
View,
Text,
Dimensions,
TouchableNativeFeedback,
TouchableOpacity
} from 'react-native';
import Constants from 'expo-constants';
export default class App extends Component {
render() {
const resizeMode = 'cover';
const text = '';
const { width, height } = Dimensions.get('screen');
return (
<View
style={{
flex: 1,
backgroundColor: '#eee',
paddingStart:Constants.statusBarHeight
}}
>
<StatusBar hidden />
<View
style={{
position: 'absolute',
top: 0,
left: 0,
width: width,
height: height,
}}
>
<Image
style={{
flex: 1,
resizeMode,
}}
source={require('../assets/home.png') }
/>
</View>
<TouchableOpacity onPress={() => this.props.navigation.navigate('Main')} >
<View
style={{
position: 'absolute',
bottom: 100,
left: 40,
right:50,
marginLeft:'auto',
marginRight:'auto'
}}
>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/SignInButton.png') }
/>
</View>
</TouchableOpacity>
// onPress dont work here as well
<TouchableOpacity onPress={() => this.props.navigation.navigate('Main')} >
<View
style={{
position: 'absolute',
bottom: 30,
left: 40,
right:50,
marginLeft:'auto',
marginRight:'auto'
}}
>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/LearnMoreButton.png') }
/>
</View>
</TouchableOpacity>
//here the onPress dont work
<TouchableOpacity onPress={()=>console.log("Hello")} >
<View
style={{
position: 'absolute',
bottom: 310,
left: 60,
right:60,
marginLeft:'auto',
marginRight:'auto'
}}
>
<Image
style={{
flex: 1,
resizeMode:'contain',
}}
source={require('../assets/Quote.png') }
/>
</View>
</TouchableOpacity>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'center',
}}
>
<Text
style={{
textAlign: 'center',
fontSize: 40,
}}
>
{text}
</Text>
</View>
</View>
);
}
}
I solved my issue by wraping inside "TouchableWithoutFeedback".
<TouchableWithoutFeedback
onPress={() => {
this.moveToAddNewCustomer()}
}}
>
<Image
style={styles.newImage}
source={require("~/assets/images/test123.png")}
/>
</TouchableWithoutFeedback>```
本文标签:
版权声明:本文标题:javascript - onPress event not working on views, images, touchableopacity in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741931321a2405590.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论