Skip to content

Instantly share code, notes, and snippets.

@Obooman
Created October 5, 2017 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Obooman/6bb1f0dc8a090ee6c24099ab9ee226f4 to your computer and use it in GitHub Desktop.
Save Obooman/6bb1f0dc8a090ee6c24099ab9ee226f4 to your computer and use it in GitHub Desktop.
file for StackOverflow answer.
import React, { Component } from 'react';
import { Text, View, StyleSheet,Image,TouchableOpacity } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Image
source={{ uri:"https://preview.ibb.co/e883Jw/2010_brown_bear.jpg" }}
style={ styles.imageWrapper }>
<TouchableOpacity
style={ styles.button }
onPress={ () => { alert("handler here") }}>
<Text style={ styles.text }>×</Text>
</TouchableOpacity>
</Image>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: 64,
backgroundColor: '#ecf0f1',
},
imageWrapper:{
width:200,
height:300,
borderRadius:20
},
button:{
width:30,
height:30,
backgroundColor:'red',
alignItems:'center',
justifyContent:'center',
overflow:'hidden',
borderRadius:20,
position:'absolute',
left:20,
top:20
},
text:{
fontSize:40,
color:'white',
lineHeight:42
}
});
@minhng22
Copy link

Error. The component cannot contain children. If you want to render content on top of the image, consider using the

@Jithinqw
Copy link

Image tag does not allow nested children components or tags. Use ImageBackground instead

@Obooman
Copy link
Author

Obooman commented Jun 29, 2019

since then react native has changed a lot, replace component with component will be fine.

import React, { Component } from 'react';
import { Text, View, StyleSheet,ImageBackground,TouchableOpacity } from 'react-native';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <ImageBackground 
          source={{ uri:"https://preview.ibb.co/e883Jw/2010_brown_bear.jpg" }}
          style={ styles.imageWrapper }>
          <TouchableOpacity 
            style={ styles.button } 
            onPress={ () => { alert("handler here") }}>
            <Text style={ styles.text }>×</Text>
          </TouchableOpacity>
        </Image>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: 64,
    backgroundColor: '#ecf0f1',
  },
  imageWrapper:{
     width:200,
     height:300,
     borderRadius:20 
  },
  button:{
    width:30,
    height:30,
    backgroundColor:'red',
    alignItems:'center',
    justifyContent:'center',
    overflow:'hidden',
    borderRadius:20,
    position:'absolute',
    left:20,
    top:20
  },
  text:{
    fontSize:40,
    color:'white',
    lineHeight:42
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment