How to access child views in React Native? - react-native

I am new to react native.
I have used a Flat Grid and I want to access a Image component on click event. I want to update its image and I am unable to access that image tag.
On click of gird item I want to update image src.
Scenario :-
I have got list of recipes and on click of these I want show them selected by making the Icon to tick.
below is Flat Grid I have used.
<FlatGrid
itemDimension={130}
items={this.state.recipie_listing}
renderItem={({ item, index }) => (
<TouchableOpacity
onPress={() => this.AddRecipe(index, item.id)}>
<View>
<ImageBackground
style={{
height: 250,
width: "100%"
}}
imageStyle={{ borderRadius: 10 }}
source={{
uri: item.image
}}
>
<Icon
name={this.state.icon_name}
style={{
color: this.state.icon_color,
position: "absolute",
right: 2,
top: 2
}}
/>
<View
style={{
backgroundColor: "rgba(220, 222, 224, 0.8)",
height: 60,
width: "100%",
borderBottomEndRadius: 10,
borderBottomStartRadius: 10,
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
position: "absolute",
bottom: 0
}}
>
<CustomText
style={{
marginStart: 10,
marginEnd: 5,
color: "white",
fontSize: 15,
fontWeight: "bold"
}}
>
{item.name}
</CustomText>
</View>
</ImageBackground>
</View>
</TouchableOpacity>
)}
/>

At first, add a state variable like showImage to false and then
In AddRecipe method call the setState method and toggle the variable value and then where you want to load the image src add conditional rendering
here is a code example
state = { showImage : false}
------
AddRecipe = () =>{
this.setState({
showImage:!this.state.showImage
})
render(){
return(
----------
<ImageBackground
style={{
height: 250,
width: "100%"
}}
imageStyle={{ borderRadius: 10 }}
source={{
uri: this.state.showImage === true ? src1 : src2
}}
>
----------
)
}
I think that will help you as far as I understand your problem.

Related

How to make this text view in react native, a tag before text view, and text can be multi-line

I want to make this text view in react native, a tag before text view, and text can be multi-line. But react native not like h5 flexbox, text alway start at the beginning of the line.
I want to make.
Trying this way
function App() {
return (
<View>
<View
style={{
flexWrap: "wrap",
width: 500,
flexDirection: "row",
backgroundColor: "pink",
}}
>
<View style={{ width: 50, height: 20, backgroundColor: "red" }} />
<View style={{ width: 50, height: 20, backgroundColor: "red" }} />
<Text>Test 123 </Text>
<Text>Test 123 </Text>
....
<View style={{ width: 50, height: 20, backgroundColor: "red" }} />
</View>
</View>
);
}
Example code Here

How do I get a image link as input and make it appear on the view component below?

class Newsfeed extends React.Component{
state = {
text: ''
};
render(){
return (
<View>
<Text style={{fontSize: 50}}>Junior Facebook</Text>
<View style={{flex: 1, flexDirection: "column"}} />
<View style={{top: 20, marginLeft: 0, width: 300, height: 180, backgroundColor: "lightblue"}}>
<TextInput
style={{
height: 150,
borderStyle: "solid",
borderWidth: 2,
fontSize: 30
}}
placeholder="New Post"
/>
<TouchableOpacity
style={{ backgroundColor: "green", marginLeft: 220, width: 80, height: 40 }}
>
<Text style={{fontSize: 24}}>Enter</Text>
</TouchableOpacity>
</View>
<View style={{marginTop: 30, marginLeft: 0, width: 300, height: 180, backgroundColor: "pink"}} >
<TouchableOpacity style={{width: 65, height: 45, marginLeft: 260}}><Text>Share</Text></TouchableOpacity>
<TouchableOpacity style={{width: 65, height: 45, marginLeft: 220, marginTop: -45}}><Text>Like</Text></TouchableOpacity>
</View>
<View style={{marginTop: 10, marginLeft: 0, width: 300, height: 130, backgroundColor: "yellow"}} >
</View>
</View>
)
}
}
The above is my current code. When I type in an image link in TextInput, I want the image to appear on the yellow View Component below. I have tried many different ways but it still does not work. How can I do so?
Thank you
#高鵬翔 answer is perfect, but said that you want to display image after clicking on 'enter' button. So here is my solution :
...
...
import { View, Text, Image } from "react-native";
...
...
//state
this.state = {
link: "",
enteredImageUri: "",
};
render() {
return (
<View>
...
...
<View
style={{
top: 20,
marginLeft: 0,
width: 300,
height: 180,
backgroundColor: "lightblue",
}}
>
<TextInput
style={{
height: 150,
borderStyle: "solid",
borderWidth: 2,
fontSize: 30,
}}
placeholder="New Post"
value={this.state.link}
onChangeText={(link) => this.setState({ link })}
/>
<TouchableOpacity
style={{
backgroundColor: "green",
marginLeft: 220,
width: 80,
height: 40,
}}
onPress={() => {
this.setState({
enteredImageUri: this.state.link,
});
}}
>
<Text style={{ fontSize: 24 }}>Enter</Text>
</TouchableOpacity>
</View>
<View>
{/* Image View */}
{this.state.enteredImageUri === "" ? (
<Text>No Image link entered</Text>
) : (
<Image
source={{ uri: this.state.enteredImageUri }}
style={{ width: 400, height: 400 }}
/>
)}
</View>
...
...
</View>
);
}
As you can see I have just assigned textinput text into the another state variable enteredImageUri, which will be used to display image.
You have to use Image Tag to show image.
Like this:
import {Image} from 'react-native';
...
<Image source={{uri: 'https://reactjs.org/logo-og.png'}}
style={{width: 400, height: 400}} />
And you have to store the text input as a state to control Image as #Will said.
So you have to add a state and the text input should like this:
constructor(props) {
super(props);
this.state = { text: '' };
}
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={(text) => this.setState({text})}
value={this.state.text}
/>
And see how you want to control Image view to show something like this, but this may be error if the uri haven't finish? Based on when you pass the correct uri to let it show, but it's an another question:
import {Image} from 'react-native';
...
<Image source={{uri: this.state.text}}
style={{width: 400, height: 400}} />

Am I doing something wrong in my View that the flexDirection doesn't work?

I'm trying to get the text to display to the right of the image, but for some reason flexDirection isn't working the way I am using it.
I've moved the flexDirection property around. Also have tried stuff other than View.
<View
style={{ flexDirection: 'row' }}
style={{
flex: 1,
backgroundColor: this.props.index % 2 == 0 ? '#420225' : '#69053e'
}}
>
<Image
source={{ uri: this.props.item.imageUrl }}
style={{ width: 100, height: 100, margin: 5 }}
/>
<Text style={{ color: 'white' }}>{this.props.item.textContent}
</Text>
</View>
I'm trying to get the Text to display to the right of the Image.
I change your code a little. You should not have 2 styles for your View.
<View
style={{
flex: 1,
flexDirection: 'row' ,
}}>
<Image
source={{ uri: "https://url.com" }}
style={{ width: 100, height: 100, margin: 5 }}
/>
<Text>something
</Text>
</View>

How to add "edit" icon as a button to <Image> component in react-native

I wanna add an "Edit" button with an icon in my <Image> component. following is my code for the <View> that contain the image.
<View style={{ flex: 1 }}>
<View
style={{
justifyContent: 'space-evenly',
alignItems: 'center',
flexDirection: 'row',
paddingVertical: 10
}}
>
<Image
source={{ uri: 'https://api.adorable.io/avatars/285/test#user.i.png' }}
style={{
marginLeft: 10, width: 100, height: 100, borderRadius: 50
}}
/>
</View>
</View>
it is much better if i can do this, without replacing <ListItem> with <Image>
You can try it like:
<View>
<Image
source={{ uri: 'https://api.adorable.io/avatars/285/test#user.i.png' }}
style={{
marginLeft: 10, width: 100, height: 100, borderRadius: 50
}}
/>
<Icon name={'edit'} containerStyle={styles.icon} onPress={console.log('I was clicked')}/>
</View>
Then the icon style like below: Note the absolute position attribute
icon: {
backgroundColor: '#ccc',
position: 'absolute',
right: 0,
bottom: 0
}
Tested with Icon from react-native-elements but I guess it should work with any other Icon.
Try to use ImageBackground to wrap your icon inside it and for icon purpose use react-native-vector-icon library.
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Icon
name="edit"
size={18}
onPress={this.edit}
>
Edit
</Icon>
</ImageBackground>
You can use react-native-vector-icons to add an icon within the view which contains image. You can also add an icon button component using that library which will look something like this
<Icon.Button
name="edit"
backgroundColor="#3b5998"
onPress={this.edit}
>
Edit
</Icon.Button>

Background image disappears after navigating back and forth several times React native

I have made a simple view with a background image and some icons on top of it. Clicking on icons navigate to different pages. It is working fine. Problem is when I navigate to other pages come back to home page and do this for 7-8 times the background image just disappears from all the screens. Below is the code of home screen and Screenshots
<Image
source={require('../images/background.jpg')}
style={{
justifyContent:'center',
resizeMode: "stretch",
height: height,
width: width,
alignItems: "center"
}} >
<StatusBar
backgroundColor="#4e0870"
barStyle="light-content"
/>
<Image style={{ height: 125, width: 125 }} source={require('../images/guru_logo.png')} />
<View style={{
marginTop: 30,
flexDirection: 'row'
}}>
<TouchableOpacity activeOpacity={.5} onPress={() => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
navigate("LiveTV")
}
}
>
<Image
source={require('../images/live.png')}
style={{
resizeMode: "stretch",
height: 75,
width: 75
}} /></TouchableOpacity>
<TouchableOpacity activeOpacity={.5} onPress={() => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
navigate("VideoPage")
}}>
<Image
source={require('../images/youtube.png')}
style={{
marginTop: 5,
resizeMode: "stretch",
marginLeft: 100,
height: 75,
width: 75
}} />
</TouchableOpacity>
</View>
<View
style={{
flexDirection: 'row',
marginTop: 20
}}>
<Text
style={{
marginLeft: -5,
fontSize: 20,
color: "#FFF"
}}>Live Tv</Text>
<Text
style={{
fontSize: 20,
color: "#FFF",
marginLeft: 125
}}>Video</Text>
</View>
<View
style={{
flexDirection: 'row',
marginTop: 20
}}>
<TouchableOpacity activeOpacity={.5} onPress={() => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
navigate("FacebookPage")
}}>
<Image
source={require('../images/facebook-logo.png')}
style={{
resizeMode: "stretch",
height: 75,
width: 75
}} /></TouchableOpacity>
<TouchableOpacity activeOpacity={.5} onPress={() => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
navigate("AudioPage")
}}>
<Image
source={require('../images/icon.png')}
style={{
resizeMode: "stretch",
marginLeft: 100,
height: 75,
width: 75
}} /></TouchableOpacity>
</View>
<View
style={{
flexDirection: 'row',
marginTop: 20
}}>
<Text
style={{
marginLeft: -20,
fontSize: 20,
color: "#FFF"
}}>Facebook</Text>
<Text
style={{
fontSize: 20,
color: "#FFF",
marginLeft: 110
}}>Audio</Text>
</View>
<TouchableOpacity activeOpacity={.5} onPress={() => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
navigate("ContactPage")
}}>
<Image
source={require('../images/contact.png')}
style={{
marginTop: 20,
resizeMode: "stretch",
height: 75,
width: 75
}} /></TouchableOpacity>
<Text style={{
fontSize: 20,
color: "#FFF"
}}>Contact Us</Text>
</Image>
Every time you navigate to that screen, it calls a re-render. I work around i've found in the past is to define your image sources as a variable, and simply reference it in the source = {HERE} section. I've provided some sample code below. This has fixed this issue in the past, let me know how it goes.
var images = {
live: {img: require('../images/live.png')},
guru: {img: require('../images/guru_logo.png')},
background: {img: require('../images/background.jpg')},
//and so on
}
class SelectionScreen extends Component {
//all your other code
and then in your Image components
<Image source = {images[background].img}.../>
I think it could be related to this react-native issue: https://github.com/facebook/react-native/issues/13600
I would try to:
Run the app on different phones / emulators to see if it depends on
device specifications (like RAM and others). If the app is for both Android and iOS I would also try to see if it's same behaviour on both platforms;
Replace temporary the images with some other smaller/larger images to see if it changes somehow the issue;
Upgrade React Native if necessary and use BackgroundImage component for background images instead of Image.