I have a prop where
console.log(this.props.userList);
returns
Array [
Object {
"userData": Object {
"userName": "David",
"userAge": 20,
},
"id": "ax3",
},
Object {
"userData": Object {
"userName": "Phillip",
"userAge": 27,
},
"id": "xq4",
},
Object {
"userData": Object {
"userName": "Kelly",
"userAge": 28,
},
"id": "pj7"
}
]
I am trying to generate a Flatlist from that data by writing:
<FlatList
data={this.props.userList}
keyExtractor={item => item.id}
renderItem={({ item }) =>
<Text>{item.userData.userName}</Text>
}
/>
When testing through Expo on my iphone, the app just freezes. Doesn't throw any errors or anything. Is this method incorrect?
*Adding in full render below
render() {
return (
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.mainViewStyle}>
<View style={styles.searchBarViewStyle}>
<TextInput
style={styles.textInputStyle}
placeholder="User"
autoCorrect={false}
autoCapitalize="none"
onChangeText={this.onUserChanged.bind(this)}
value={this.props.userInput}
/>
</View>
<View>
<Button
title="press this"
onPress={() =>
this.props.navigation.navigate("yearSearch")
}
/>
</View>
<View>
<Button
title="testUserSearch"
onPress={() => this.showData()}
/>
</View>
<View>
<FlatList
data={this.props.userList}
keyExtractor={item => item.id}
renderItem={({ item }) => (
<Text>{item.userData.userName}</Text>
)}
/>
</View>
</View>
</TouchableWithoutFeedback>
);
}
If I take out the Flatlist part of it, it runs fine. The showData() function currently just console logs this.props.userList, which returns the array post in the beginning.
you can flatten your array of object in one array (place all the objects in one array after receiving using some loop) and then use it in flat list (since you know how many entries each object has). to understand it further see the correct answer here.
react native flatlist with several arrays of data
Related
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>
I just started learning react native. I am currently trying to implement TouchableOpacity on image rendering from an array and use the info for that element from the array to create a new page. The goal of the code is to have a userInfo page that lists all the images and once you tap on an image, it will show details of that user (you will see just this user and no other ones) using userDetails. I know my TouchableOpacity part will not work, but I don't know how to make it work.
This is from my userInfo.js:
const UserInfo =() => {
const users = [
{userName: "A",
imageSource: require("../../assets/users/A.jpg"),
bio: "this is A"
},
{userName: "B",
imageSource: require("../../assets/users/B.jpg"),
bio: "this is B"
},
{userName: "C",
imageSource: require("../../assets/users/C.jpg"),
bio: "this is C"
}
]
return(
<FlatList
showsVerticalScrollIndicator={false}
keyExtractor={(users)=> users.userName}
data={users}
renderItem = {({item})=>{
return <View>
<TouchableOpacity onPress={userInfoLink(users.userName, users.imageSource, users.bio)}>
<View style={styles.viewBox}><Image style={styles.image} source={item.imageSource}/></View>
</TouchableOpacity>
<Text style={styles.text}>{item.userName}</Text>
</View>;
}}
/>
)
};
This is from my userDetails.js:
const UserDetail=({imageSource, userName, bio}) => {
return <View>
<View style={styles.viewBox}><Image style={styles.image} source={imageSource}/></View>
<Text style={styles.text}>User name: {userName}</Text>
<Text style={styles.text}>Bio: {bio}</Text>
</View>;
}
Ok, after doing some research I found a way to make it work. I used navigation.navigate and passing parameters into the page.
Here is my return code in UserInfo.js:
return(
<FlatList
showsVerticalScrollIndicator={false}
keyExtractor={(users)=> users.userName}
data={users}
renderItem = {({item})=>{
return <View>
<TouchableOpacity onPress={()=>{navigation.navigate('UserDetails', {imageSource: item.imageSource, userName: item.userName, bio:item.bio })}}>
<View style={styles.viewBox}><Image style={styles.image} source={item.imageSource}/></View>
</TouchableOpacity>
<Text style={styles.text}>{item.userName}</Text>
</View>;
}}
/>
)
Here is the code in UserDetails.js:
const UserDetail=({navigation}) => {
const imageSource = navigation.getParam('imageSource', 'a no image image');
const userName = navigation.getParam('userName', 'username error');
const bio = navigation.getParam('bio', 'bio error');
return <View>
<Text style={styles.title}>User Detail</Text>
<View style={styles.viewBox}><Image style={styles.image} source={imageSource}/></View>
<Text style={styles.text}>User name: {userName}</Text>
<Text style={styles.text}>Bio: {bio}</Text>
<Button title="User Info" onPress={()=>{navigation.navigate('UserInfo')}}/>
</View>;
}
I am wondering if adding the parenthesis and arrow was what fixed it.
You went from:
onPress={userInfoLink(users.userName, users.imageSource, users.bio)}
To this:
onPress={()=>{navigation.navigate('UserDetails', {imageSource: item.imageSource, userName: item.userName, bio:item.bio })}}
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/4ePzL8AAAAHdElNRQfhBAcKOBaXR3CCAAAASXRFWHRjb21tZW50AEZpbGUgc291cmNlOiBodHRwczovL2NvbW1vbnMud2lraW1lZGlhLm9yZy93aWtpL0ZpbGU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740ePdpzFWrnYCvxr+t83iGHHGLpEsMzeZk/f36QhiOGTV7Ii3NY5cBuNwQH6QD9ZiWIOLRp06ZmMxNfVg1ch7TjKCP59uzZ0/LkGGJv4nSlQUzqnIfVCflMnjzZa9GihcXvnM+chViTOgLMATiHGJX9pEc8qjKwWkQ5qVdiXtepbIJNGuHjON8RylaMsMuNdrbiov3ouPW4U6dO3h577GExNvupj9mzZ5eqD4GyYrcL76ztmZNwLKspLC4ANzJ7t99+u+VDmWnnxo0be25Et1togevYtpIjW6qDUkyvrrhCVjhkuEJs8j9XFxbSRKL2SXL3h9MLp19RXuwrLy/2hc+v6vFxSUX5lmdzWCpKh7bWftcBzINzIYiQTnMU8YHQJhzjE/Nrv45h7iRwewFRAvrK2hshkcpNBAMoQJSUlXn4HBUgVzi3orRUAexD9L+sig8fH9ZzfHhfVBraj17H5KYTlvAx4fRzj0OnMkblmVt+jpEuNz3lo/256Wm/bArbGD4OKS8d7UfKSkfnMKEXmJxz3zs3UHHl9Y9//KPpmZ8572s3PXEOiwTcJ0RnIFRTGMJNb8pb+VRDIpWZZFIpEclZKQrH68TheGmtfGlOE77hjZvjIDuA8IBz6BDsj+poVZBIZSaZVFpEbu7YY1lWq0MCpIX0LD/K+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
I followed this answer to dynamically style my component.
Here is my render method :
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.images}
numColumns={2}
keyboardShouldPersistTaps={'always'}
keyboardDismissMode={'on-drag'}
keyExtractor={item => item.localIdentifier}
renderItem={({ item, index }) =>
<TouchableHighlight
underlayColor='transparent'
onPress={() => this.openImage(index)}
onLongPress={() => this.startSelection(item)}
>
<View style={[styles.albumContainer, (this.state.selectedItems.indexOf(item)>-1)?styles.selectedItem:styles.unselectedItem]}>
<Image
style={styles.albumThumbnail}
source={item.image}
/>
</View>
</TouchableHighlight>
}
/>
</View>
);
}
As you can see I am displaying image thumbnail with TouchableHighlight and FlatList. When user will press and hold on any image thumbnail I called startSelection() with particular flatlist item which then add that item to state. I used that state to set style dynamically of my image as :
<View style={[styles.albumContainer, (this.state.selectedItems.indexOf(item)>-1)?styles.selectedItem:styles.unselectedItem]}>
<Image
style={styles.albumThumbnail}
source={item.image}
/>
</View>
Here is startSelection() method :
startSelection(item) {
let temp = this.state.selectedItems;
temp.push(item);
this.setState({
selectedItems : temp
});
}
Here is my stylesheet :
const styles = StyleSheet.create({
selectedItem: {
borderWidth: 3,
borderColor: '#22aaff',
},
unselectedItem: {
borderColor: '#000000',
}
});
But when user press and hold that view, item will added to state but style is not changing.
Please help me what's going wrong here !!!
This can be found on FlatList docs:
This is a PureComponent which means that it will not re-render if props remain shallow-equal. Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes. This includes the data prop and parent component state.
So you can add extraData to your FlatList component like this:
FlatList Component:
<FlatList
data={this.state.images}
extraData={this.state} //add this!
numColumns={2}
keyboardShouldPersistTaps={'always'}
keyboardDismissMode={'on-drag'}
keyExtractor={item => item.localIdentifier}
renderItem={({ item, index }) =>
<TouchableHighlight
underlayColor='transparent'
onPress={() => this.openImage(index)}
onLongPress={() => this.startSelection(item)}
>
<View style={[styles.albumContainer, (this.state.selectedItems.indexOf(item)>-1)?styles.selectedItem:styles.unselectedItem]}>
<Image
style={styles.albumThumbnail}
source={item.image}
/>
</View>
</TouchableHighlight>
}
/>
P.S: If your component state has variables which should not re-render FlatList, you would be better of using extraData = {this.state.selectedItems}, but then you need to make sure you pass a different reference to selectedItems when you call setState on startSelection. Like this:
startSelection(item) {
let temp = [...this.state.selectedItems];
temp.push(item);
this.setState({
selectedItems : temp
});
}
Wrap them with extra []
style={[styles.albumContainer, [(this.state.selectedItems.indexOf(item)>-1)?styles.selectedItem:styles.unselectedItem]]}
I am using react-native-swiper-flatlist , I want to scroll forward to some index by tapping button but no clue how to do it. Although I am very beginner to react native development.
I have searched alot but nothing helps, I get an error of undefined is not an object this2.swiper.scrollToIndex'
render() {
return (
<View style={styles.container}>
<SwiperFlatList
ref={swiper => {
this.swiper = swiper;
}}
data={[
{ key: "a" },
{ key: "b" },
{ key: "c" },
{ key: "d" },
{ key: "e" }
]}
index={0}
renderItem={({ item, index }) => (
<View>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Button
title={"Next"}
onPress={this.swiper.scrollToIndex(1, true)}
style={{ backgroundColor: "white" }}
/>
</View>
)}
/>
</View>
);
}
Should swipe on button click
Here is the screenshot of the error I am getting
Try adding ref='swiper' as a SwiperFlatList prop,
Example
<SwiperFlatList ref='swiper'/>
this.refs.swiper._scrollToIndex(2)
For those who are using React hook instead of class, you can do sth like that to utilise the scrollToIndex function
//First, make a ref for storing the swiper instance inside your swiper compoent
const swiperRef = useRef<any>({});
//Second, store the reference
<SwiperFlatList
ref={(component) => { swiperRef.current._swiper = component; }}
>
//Later on, you can call the function like this
swiperRef.current._swiper.scrollToIndex({index: 2})