Displaying the list of pictures from downloaded url - react-native

How I can display a list of pictures? I get the list URL ( local address) from DB.
[
{"employeId": 1, "name": "Mariusz", "url": "../assets/servicesIcons/baleyage.png"},
{"employeId": 2, "name": "Mariola", "url": "../assets/servicesIcons/baleyage.png"},
{"employeId": 3, "name": "Rafal", "url": "../assets/servicesIcons/baleyage.png"}
]
const HoursComponent = ({id, time}) => {
const TimewithoutLast3 = time.slice(0, -3);
return (
<View style={{...styles.rootContainer, backgroundColor: bgColor}}>
<TouchableOpacity >
<Text style={styles.buttonText} onPress={onPress}>{TimewithoutLast3}</Text>
</TouchableOpacity>
</View>
);
}
<FlatList
numColumns={3}
data={employee}
keyExtractor={item => item.id}
renderItem={({ item }) => (<EmployeeComponet {...item} />)}></FlatList>
I try using require but it doesn't work

I try add somthing like this but is not working
example url value '../../assets/servicesIcons/test.png'
<Image source={{require:("\'" + url + "\'")}} />
but when I added static value then is ok.
const value = require('../../assets/servicesIcons/test.png');
...
<Image source={value} />

Import Image from react native and use Image tag like
const HoursComponent = ({employeId, name, url}) => {
const TimewithoutLast3 = time.slice(0, -3);
return (
<View style={{...styles.rootContainer, backgroundColor: bgColor}}>
<TouchableOpacity >
<Text style={styles.buttonText} onPress={onPress}>{name}</Text>
<Image source={{uri :url}} />
</TouchableOpacity>
</View>
);
}
<FlatList
numColumns={3}
data={employee}
keyExtractor={item => item.employeId}
renderItem={({ item }) => (<EmployeeComponet {...item} />)}></FlatList>

Related

Flatlist simulating different pages

I have to build 5 pages but as they have all the same layout I want to use a flatlist to populate the fields, my doubt us vary basic, I read the documentation and had problems to fully understand, here is my code
const DATA = [
{
picture: '../../Components/Images/wellbeing.png',
text1: 'Get weekly overviews and find',
text2: 'out whats impacting your health',
text3: 'and wellness',
percent: 20,
},
];
const Item = ({picture, text1, text2, text3, percent}) => (
<View style={styles.container}>
<View>
<Image source={require({picture})} style={styles.image} />
</View>
<View style={styles.text_field}>
<Text style={styles.textContent}> {text1} </Text>
<Text style={styles.textContent}> {text2} </Text>
<Text style={styles.textContent}> {text3} </Text>
</View>
<View style={styles.footer}>
<TouchableOpacity onPress={() => navigation.navigate('HabitTracking')}>
<ProgressCircle
percent={{percent}}
radius={30}
borderWidth={3}
color="#3399FF"
shadowColor="white"
bgColor="blue">
<Icon name="arrowright" size={25} color="white"></Icon>
</ProgressCircle>
</TouchableOpacity>
</View>
</View>
);
I'm having trouble on how to populate this in the class, here is what I have so far
const Wellcome = () => {
const renderItem = ({picture, text1, text2, text3, percent}) => (
<Item picture={Item.picture}
/>
);
return (
);
};
export default Wellcome;
am I doing everything wrong?
please let me know if you need any further information
const renderItem = (item) => (
<Item picture={item.picture} />
);
and call renderItem in your render method
...
{renderItem(item)}
...
where item looks something like...
{
picture: 'something',
text1: 'something',
text2: 'something',
text3: 'something',
percent: 'something'
}

How can I change the numColumns of Flatlist when changing the orientation

I created the function name getOrientation() and put it at useEffect so that whenever I rotate the device, it will rerender the component and show me which orientation the device is.
I also created the variable to determine the orientation by using hook
getOrientation()
const [orientation, setOrientation] = useState("")
const window = useWindowDimensions()
const getOrientation = () => {
if (window.height < window.width) {
setOrientation("LANDSCAPE")
} else {
setOrientation("PORTRAIT")
}
return orientation
}
at useEffect
useEffect(() => {
getOrientation()
})
console.log(orientation)
My problem is that I want to set the numsColumns = 2 in Flatlist (LANDSCAPE) and equals to 1 for the portrait mode but the error popup telling me that I cannot change the numColumns on the fly. What should I do ?
Here is my flatlist
<View style={styles.container}>
<FlatList
contentContainerStyle={{
paddingLeft: insets.left,
paddingRight: insets.right,
}}
data={dishes.dishes}
numColumns={orientation == "LANDSCAPE" ? 2 : 1}
renderItem={({ item, index }) => (
<Tile
style={styles.tileItem}
key={index}
title={item.name}
caption={item.description}
onPress={() => navigation.navigate('Dishdetail_Screen', { dishId: item.id })} // passing infor from one to another screen
// chevron={false}
featured
imageSrc={{
uri: baseUrl + item.image
}}
/>
)}
keyExtractor={item => item.id.toString()} />
</View>
This creepy error
enter image description here
P/s: I'm the fresh React-Native developer. Thanks everyone who is checking my problems
Try adding a key prop to your Flatlist like so with the value being ur orientation:
<FlatList
key={orientation} // add key prop here
contentContainerStyle={{
paddingLeft: insets.left,
paddingRight: insets.right,
}}
data={dishes.dishes}
numColumns={orientation == "LANDSCAPE" ? 2 : 1}
renderItem={({ item, index }) => (
<Tile
style={styles.tileItem}
key={index}
title={item.name}
caption={item.description}
onPress={() => navigation.navigate('Dishdetail_Screen', { dishId: item.id })} // passing infor from one to another screen
// chevron={false}
featured
imageSrc={{
uri: baseUrl + item.image
}}
/>
)}
keyExtractor={item => item.id.toString()} />

How can create a require() source with custom path and the output of an arraw prop?

I have this array of data in a .js file:
export const EXPLORE_CATEGORIES = [
{
id: 1,
name: 'E-Scooters',
groups: 520,
iconName: 'Photo.png',
description: 'Small and big e-scooters',
},
…
]
And in my view, I render a FlatList with a component. This component loads an image as an icon.
return (
<View style={{ ...styles.screen }}>
<BodyOne style={styles.text}>This is the explore view.</BodyOne>
<FlatList
keyExtractor={item => item.id}
data={EXPLORE_CATEGORIES}
renderItem={itemData => (
<TwoLineWithIcon
icon={require('../../assets/images/icons/' + itemData.item.iconName)}
title={itemData.item.name}
subtitle={itemData.item.description}
caption={itemData.item.groups + ' groups'}
/>
)}
/>
</View>
);
The component has source={props.icon} so, in my FlatList's renderItem icon={...} prop I would like to chain both path and iconName but failed miserably.
this works:
icon={require('../../assets/images/icons/Photo.png'}
But I would like to do this:
icon={require('../../assets/images/icons/' + itemData.item.iconName)}
Is this possible?
Dynamic require/imports are not supported in react-native. You might want to modify your code as below
export const EXPLORE_CATEGORIES = [
{
id: 1,
name: 'E-Scooters',
groups: 520,
iconName: require('../../assets/images/icons/Photo.png'),
description: 'Small and big e-scooters',
},
…
]
return (
<View style={{ ...styles.screen }}>
<BodyOne style={styles.text}>This is the explore view.</BodyOne>
<FlatList
keyExtractor={item => item.id}
data={EXPLORE_CATEGORIES}
renderItem={itemData => (
<TwoLineWithIcon
icon={itemData.item.iconName}
title={itemData.item.name}
subtitle={itemData.item.description}
caption={itemData.item.groups + ' groups'}
/>
)}
/>
</View>
);

how could I read the data and the image from this json file into flatlist?

Please I'm struggling to read this data of the json file on a simple flatlist, I tried to do item.content or item.description and item.medias but it doesnt work please could someone help me
[
{
"medias": [
"iVBORw0KGgoAAAANSUhEUgAAALYAAACvCAYAAABQKSRXAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAAmJLR0QA/4ePzL8AAAAHdElNRQfhBAcKOBaXR3CCAAAASXRFWHRjb21tZW50AEZpbGUgc291cmNlOiBodHRwczovL2NvbW1vbnMud2lraW1lZGlhLm9yZy93aWtpL0ZpbGU6RFhDX1RlY2gucG5n4Fgx2gAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxNy0wNC0wN1QxMDo1NjoyMiswMDowMD/Nvz8AAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTctMDQtMDdUMTA6NTY6MjIrMDA6MDBOkAeDAAAATXRFWHRzb2Z0d2FyZQBJbWFnZU1hZ2ljayA2LjguOS05IFExNiB4ODZfNjQgMjAxNy0wMy0xMiBodHRwOi8vd3d3LmltYWdlbWFnaWNrLm9yZ6wvQ+0AAAAYdEVYdFRodW1iOjpEb2N1bWVudDo6UGFnZXMAMaf/uy8AAAAYdEVYdFRodW1iOjpJbWFnZTo6SGVpZ2h0ADUwMHiYA+wAAAAXdEVYdFRodW1iOjpJbWFnZTo6V2lkdGgANTAw62lTsQAAABl0RVh0VGh1bWI6Ok1pbWV0eXBlAGltYWdlL3BuZz+yVk4AAAAXdEVYdFRodW1iOjpNVGltZQAxNDkxNTYyNTgywFnCLQAAABN0RVh0VGh1bWI6OlNpemUAMTIuNktCQvrONG4AACRvSURBVHhe7Z0H2BXF9cZXExQEK4INRKUoKiAaSKKiAlFiRBAV7MEeQSNqVAKKGCXG8hBjRQ0WoiEWMGoAMQFiiYWmUgUFBFsQFa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3M8t9ayioOn5k5LJtHy5lGjS1Wk3MljhgyVhSOiTSQdcb02bdrYxSAWHZgYcsWWSTEX0Byp7XjHd1tUYPGA4ziGq85MUDmG/ZuDjNgZagyQG+SulAiQnn1lkbai86uCjNgZahwQVCQFELksQnOcPHRNEFrIiJ0hkSj7zpwMGbZgZMTOkEB43v8Bsk6/fP4dm60AAAAASUVORK5CYII="
],
"description": "ccc",
"id": 3,
"user": {
"id": 1,
"nom": "user",
"prenom": "user",
"tel": "2126548390",
"url": null,
"departement": "RH",
"username": "admin"
},
"content": "cccc"
}
]
and this is my code (I want to display the image too )
<FlatList
style={{ marginTop: 20 }}
data={this.state.data}
refreshing={this.state.isLoading}
onRefresh={this.makeRemoteRequest}
renderItem={({ item, index })=>
<View>
{item.published == true ? <Card>
<TouchableOpacity onPress={this.onRemove}>
<Animated.View style={rowStyles}>
<View>
<Text style={styles.email}>{item.data[0].content}</Text>
</View>
</Animated.View>
<Animated.View style={rowStyles}>
<Text style={styles.email}>{item.description}</Text>
</Animated.View>
</TouchableOpacity></Card> : <Text></Text> }
</View>
}
keyExtractor={(item) => item.id}
ItemSeparatorComponent={this.renderSeparator}
ListHeaderComponent={this.renderHeader}
ListFooterComponent={this.renderFooter}
/>
First of all, keyExtractor expects a return of string and you are supplying an int (id);
You could either change your id in the data store to a string if possible, or do a map to convert the id to a string or you could do this:
keyExtractor={(item) => item.id.toString()}
which I don't recommend because it will always be rerun on render.
Also if the data from json is not changing, you could put it on the class as shown below:
The choices are yours but I believe the keyExtractor is your major issue.
// read the json file on import
const jsonData = require('path/to/file.json');
// set the data on mount to convert id to string
this.dataWithStringId = jsonData.map((item) => ({
...item,
id: item.id.toString(),
}));
render() {
return (
<FlatList
keyExtractor={(item) => item.id}
data={this.dataWithStringId}
renderItem={({item}) => (
<Card>
<TouchableOpacity>
<View>
<View>
<Text>{item.content}</Text>
</View>
</View>
<Animated.View>
<Text>{item.description}</Text>
</Animated.View>
</TouchableOpacity>
</Card>
)}
/>
)
}
EDIT:
here is a link to keyExtrctor

React Native flatlist conditional rendering

I have the following flat list in react native with the following
items.
key
name
type
Now i also have the following renderItem function that is used to render
the elements of the flatlist.
renderItem={({ item }) => (
<View>
<View style={styles.navBarLeftButton}>
<Avatar
medium
rounded
source={{uri:item.name}}
activeOpacity={0.7}
onPress={() => console.log(this.state.data)}
/>
<Text style={styles.textbutton}>{item.type}</Text>
<Text>{item.description}</Text>
<Text>{item.request} <Emoji name={item.request} style={{fontSize: 15}} />
<Emoji name="pray" style={{fontSize: 15}} /></Text>
</View>
</View>
)}
I want to render a different render function base on the item key of the flatlist
Is there away i can do conditional rendering with react native flatlist base
on key?
The renderItem prop for Flatlist can accept 2 arguments, the second being index, so you can do something like
renderItem={({ item, index })=>{
if(index = 0){
//do something
}
}}
Then just throw in a switch or some if statements and you can render conditionally.
Based on 'theme' values in variable DATA (as keys) FlatList renderItem prop is conditionally accepting different Views/Components returned by separate functions
<FlatList
data={DATA}
renderItem={({ item, index }) => {
if (item.theme === 1) {
return this.renderTheme1({ item });
}
return this.renderTheme2({ item });
}}
keyExtractor={item => item.id}
/>
This works for me:
renderItem={({ item }) => (
item.isDeleted == false ?
<View>
<Activity isRunning={item.isRunning} />
</View>
: null
)}
keyExtractor={(item) => item.title}
/>
Are you looking to return two different template sets? Something like:
renderItem={({item, index}) => {
if (index == 0) {
return <View style={{flex: 1, flexDirection: 'row'}}>
<Image source={item.image} style={styles.flatList_imageView} resizeMode="contain"/>
</View>
}
return <View style={{flex: 1, flexDirection: 'row'}}>
<Text>{item.key}</Text>
</View>
}
I think it will help you
renderItem={({ item, index })=>
<View>
{index == 0 ? <Text>Some Text</Text> : <Text>Some Text</Text> }
</View>
}}