FlatList Rerendering Issue
up vote
0
down vote
favorite
I know this has been asked multiple times, but none of the solutions mentioned seem to be working for me.
import React, { Component } from "react";
import { Text, View, StyleSheet, FlatList } from "react-native";
import { Fonts, FontSize, Colors } from "../../constants";
const styles = StyleSheet.create({
chat: {
flex: 1,
backgroundColor: "#EDECEA",
display: "flex",
alignItems: "center"
//justifyContent: "center"
},
introText: {
...Fonts.bold,
fontSize: FontSize.s,
maxWidth: 200,
lineHeight: 18.5,
textAlign: "center",
marginTop: 100,
marginBottom: 100,
color: "rgba(28, 28, 29, 0.5)"
},
smallText: {
...Fonts.normal
},
servicePackContentContainer: {
position: "absolute",
backgroundColor: "red",
shadowColor: "gray",
shadowOffset: {
width: 0,
height: 0.5
},
shadowRadius: 1,
borderBottomWidth: 1,
borderBottomColor: "rgba(217, 216, 215, 0.5)"
},
serviceTitleStyle: {
fontSize: 22,
...Fonts.bold,
textAlign: "center",
color: Colors.white
},
serviceDescStyle: {
textAlign: "center",
...Fonts.bold,
fontSize: 14,
marginHorizontal: 10,
color: Colors.white
}
});
class MyOwnList extends Component {
constructor(props) {
super(props);
this.state = {
formattedData: ,
data: [
{ id: 0, title: "Option0"},
{ id: 1, title: "Option1"},
{ id: 2, title: "Option2"},
{ id: 3, title: "Option3"},
{
id: 4,
title: "Option4"
},
{ id: 5, title: "Option5"},
{ id: 6, title: "Option6"},
{ id: 7, title: "Option7"},
{ id: 8, title: "Option8"},
{ id: 9, title: "Option9"}
]
};
}
componentWillMount() {
this.generateTheList();
}
getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
async generateTheList() {
let len = this.state.data.length;
for (let i = 0; i < 4; i++) {
let randomIndex = this.getRandomInt(0, len);
await this.setState({
formattedData: [
...this.state.formattedData,
this.state.data[randomIndex]
]
});
}
}
renderServices = item => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
render() {
return (
<View style={styles.chat}>
<Text style={styles.introText}>
Ask for our help and we will answer as soon as possible
<Text style={styles.smallText}> (English only)</Text>
</Text>
<FlatList
ref={component => {
this.listView = component;
}}
data={this.state.formattedData}
renderItem={this.renderServices}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}
export default MyOwnList;
Basically, I want to display 4 random options whenever I come to this screen. As setState is async, I added asyn and await to the methods so that when the state changes it is reflected on the screen. This does work but at times I get
undefined is not an object evaluating('item.title').
react-native
add a comment |
up vote
0
down vote
favorite
I know this has been asked multiple times, but none of the solutions mentioned seem to be working for me.
import React, { Component } from "react";
import { Text, View, StyleSheet, FlatList } from "react-native";
import { Fonts, FontSize, Colors } from "../../constants";
const styles = StyleSheet.create({
chat: {
flex: 1,
backgroundColor: "#EDECEA",
display: "flex",
alignItems: "center"
//justifyContent: "center"
},
introText: {
...Fonts.bold,
fontSize: FontSize.s,
maxWidth: 200,
lineHeight: 18.5,
textAlign: "center",
marginTop: 100,
marginBottom: 100,
color: "rgba(28, 28, 29, 0.5)"
},
smallText: {
...Fonts.normal
},
servicePackContentContainer: {
position: "absolute",
backgroundColor: "red",
shadowColor: "gray",
shadowOffset: {
width: 0,
height: 0.5
},
shadowRadius: 1,
borderBottomWidth: 1,
borderBottomColor: "rgba(217, 216, 215, 0.5)"
},
serviceTitleStyle: {
fontSize: 22,
...Fonts.bold,
textAlign: "center",
color: Colors.white
},
serviceDescStyle: {
textAlign: "center",
...Fonts.bold,
fontSize: 14,
marginHorizontal: 10,
color: Colors.white
}
});
class MyOwnList extends Component {
constructor(props) {
super(props);
this.state = {
formattedData: ,
data: [
{ id: 0, title: "Option0"},
{ id: 1, title: "Option1"},
{ id: 2, title: "Option2"},
{ id: 3, title: "Option3"},
{
id: 4,
title: "Option4"
},
{ id: 5, title: "Option5"},
{ id: 6, title: "Option6"},
{ id: 7, title: "Option7"},
{ id: 8, title: "Option8"},
{ id: 9, title: "Option9"}
]
};
}
componentWillMount() {
this.generateTheList();
}
getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
async generateTheList() {
let len = this.state.data.length;
for (let i = 0; i < 4; i++) {
let randomIndex = this.getRandomInt(0, len);
await this.setState({
formattedData: [
...this.state.formattedData,
this.state.data[randomIndex]
]
});
}
}
renderServices = item => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
render() {
return (
<View style={styles.chat}>
<Text style={styles.introText}>
Ask for our help and we will answer as soon as possible
<Text style={styles.smallText}> (English only)</Text>
</Text>
<FlatList
ref={component => {
this.listView = component;
}}
data={this.state.formattedData}
renderItem={this.renderServices}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}
export default MyOwnList;
Basically, I want to display 4 random options whenever I come to this screen. As setState is async, I added asyn and await to the methods so that when the state changes it is reflected on the screen. This does work but at times I get
undefined is not an object evaluating('item.title').
react-native
You got tworenderItem
s, did you mean to do that?
– Matt Jameson
Nov 7 at 10:21
removed the other one.. was trying sometihng
– Doe
Nov 7 at 10:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I know this has been asked multiple times, but none of the solutions mentioned seem to be working for me.
import React, { Component } from "react";
import { Text, View, StyleSheet, FlatList } from "react-native";
import { Fonts, FontSize, Colors } from "../../constants";
const styles = StyleSheet.create({
chat: {
flex: 1,
backgroundColor: "#EDECEA",
display: "flex",
alignItems: "center"
//justifyContent: "center"
},
introText: {
...Fonts.bold,
fontSize: FontSize.s,
maxWidth: 200,
lineHeight: 18.5,
textAlign: "center",
marginTop: 100,
marginBottom: 100,
color: "rgba(28, 28, 29, 0.5)"
},
smallText: {
...Fonts.normal
},
servicePackContentContainer: {
position: "absolute",
backgroundColor: "red",
shadowColor: "gray",
shadowOffset: {
width: 0,
height: 0.5
},
shadowRadius: 1,
borderBottomWidth: 1,
borderBottomColor: "rgba(217, 216, 215, 0.5)"
},
serviceTitleStyle: {
fontSize: 22,
...Fonts.bold,
textAlign: "center",
color: Colors.white
},
serviceDescStyle: {
textAlign: "center",
...Fonts.bold,
fontSize: 14,
marginHorizontal: 10,
color: Colors.white
}
});
class MyOwnList extends Component {
constructor(props) {
super(props);
this.state = {
formattedData: ,
data: [
{ id: 0, title: "Option0"},
{ id: 1, title: "Option1"},
{ id: 2, title: "Option2"},
{ id: 3, title: "Option3"},
{
id: 4,
title: "Option4"
},
{ id: 5, title: "Option5"},
{ id: 6, title: "Option6"},
{ id: 7, title: "Option7"},
{ id: 8, title: "Option8"},
{ id: 9, title: "Option9"}
]
};
}
componentWillMount() {
this.generateTheList();
}
getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
async generateTheList() {
let len = this.state.data.length;
for (let i = 0; i < 4; i++) {
let randomIndex = this.getRandomInt(0, len);
await this.setState({
formattedData: [
...this.state.formattedData,
this.state.data[randomIndex]
]
});
}
}
renderServices = item => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
render() {
return (
<View style={styles.chat}>
<Text style={styles.introText}>
Ask for our help and we will answer as soon as possible
<Text style={styles.smallText}> (English only)</Text>
</Text>
<FlatList
ref={component => {
this.listView = component;
}}
data={this.state.formattedData}
renderItem={this.renderServices}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}
export default MyOwnList;
Basically, I want to display 4 random options whenever I come to this screen. As setState is async, I added asyn and await to the methods so that when the state changes it is reflected on the screen. This does work but at times I get
undefined is not an object evaluating('item.title').
react-native
I know this has been asked multiple times, but none of the solutions mentioned seem to be working for me.
import React, { Component } from "react";
import { Text, View, StyleSheet, FlatList } from "react-native";
import { Fonts, FontSize, Colors } from "../../constants";
const styles = StyleSheet.create({
chat: {
flex: 1,
backgroundColor: "#EDECEA",
display: "flex",
alignItems: "center"
//justifyContent: "center"
},
introText: {
...Fonts.bold,
fontSize: FontSize.s,
maxWidth: 200,
lineHeight: 18.5,
textAlign: "center",
marginTop: 100,
marginBottom: 100,
color: "rgba(28, 28, 29, 0.5)"
},
smallText: {
...Fonts.normal
},
servicePackContentContainer: {
position: "absolute",
backgroundColor: "red",
shadowColor: "gray",
shadowOffset: {
width: 0,
height: 0.5
},
shadowRadius: 1,
borderBottomWidth: 1,
borderBottomColor: "rgba(217, 216, 215, 0.5)"
},
serviceTitleStyle: {
fontSize: 22,
...Fonts.bold,
textAlign: "center",
color: Colors.white
},
serviceDescStyle: {
textAlign: "center",
...Fonts.bold,
fontSize: 14,
marginHorizontal: 10,
color: Colors.white
}
});
class MyOwnList extends Component {
constructor(props) {
super(props);
this.state = {
formattedData: ,
data: [
{ id: 0, title: "Option0"},
{ id: 1, title: "Option1"},
{ id: 2, title: "Option2"},
{ id: 3, title: "Option3"},
{
id: 4,
title: "Option4"
},
{ id: 5, title: "Option5"},
{ id: 6, title: "Option6"},
{ id: 7, title: "Option7"},
{ id: 8, title: "Option8"},
{ id: 9, title: "Option9"}
]
};
}
componentWillMount() {
this.generateTheList();
}
getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
async generateTheList() {
let len = this.state.data.length;
for (let i = 0; i < 4; i++) {
let randomIndex = this.getRandomInt(0, len);
await this.setState({
formattedData: [
...this.state.formattedData,
this.state.data[randomIndex]
]
});
}
}
renderServices = item => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
render() {
return (
<View style={styles.chat}>
<Text style={styles.introText}>
Ask for our help and we will answer as soon as possible
<Text style={styles.smallText}> (English only)</Text>
</Text>
<FlatList
ref={component => {
this.listView = component;
}}
data={this.state.formattedData}
renderItem={this.renderServices}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}
export default MyOwnList;
Basically, I want to display 4 random options whenever I come to this screen. As setState is async, I added asyn and await to the methods so that when the state changes it is reflected on the screen. This does work but at times I get
undefined is not an object evaluating('item.title').
react-native
react-native
edited Nov 7 at 22:36
halfer
14.2k757105
14.2k757105
asked Nov 7 at 10:14
Doe
14
14
You got tworenderItem
s, did you mean to do that?
– Matt Jameson
Nov 7 at 10:21
removed the other one.. was trying sometihng
– Doe
Nov 7 at 10:24
add a comment |
You got tworenderItem
s, did you mean to do that?
– Matt Jameson
Nov 7 at 10:21
removed the other one.. was trying sometihng
– Doe
Nov 7 at 10:24
You got two
renderItem
s, did you mean to do that?– Matt Jameson
Nov 7 at 10:21
You got two
renderItem
s, did you mean to do that?– Matt Jameson
Nov 7 at 10:21
removed the other one.. was trying sometihng
– Doe
Nov 7 at 10:24
removed the other one.. was trying sometihng
– Doe
Nov 7 at 10:24
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
How come you have specified the property renderItem twice for the FlatList?
Looks like you forgot to destruct the argument to renderServices:
renderServices = ({item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
add a comment |
up vote
0
down vote
You used wrong syntax. The render item function should be:
renderServices = ({index, item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);};
What you need is access to "item" of flat list row: ( {index, item} )
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
How come you have specified the property renderItem twice for the FlatList?
Looks like you forgot to destruct the argument to renderServices:
renderServices = ({item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
add a comment |
up vote
0
down vote
How come you have specified the property renderItem twice for the FlatList?
Looks like you forgot to destruct the argument to renderServices:
renderServices = ({item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
add a comment |
up vote
0
down vote
up vote
0
down vote
How come you have specified the property renderItem twice for the FlatList?
Looks like you forgot to destruct the argument to renderServices:
renderServices = ({item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
How come you have specified the property renderItem twice for the FlatList?
Looks like you forgot to destruct the argument to renderServices:
renderServices = ({item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);
};
answered Nov 7 at 10:22
jonixj
387311
387311
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
add a comment |
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45
add a comment |
up vote
0
down vote
You used wrong syntax. The render item function should be:
renderServices = ({index, item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);};
What you need is access to "item" of flat list row: ( {index, item} )
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
add a comment |
up vote
0
down vote
You used wrong syntax. The render item function should be:
renderServices = ({index, item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);};
What you need is access to "item" of flat list row: ( {index, item} )
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
add a comment |
up vote
0
down vote
up vote
0
down vote
You used wrong syntax. The render item function should be:
renderServices = ({index, item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);};
What you need is access to "item" of flat list row: ( {index, item} )
You used wrong syntax. The render item function should be:
renderServices = ({index, item}) => {
return (
<View style={styles.servicePackContentContainer}>
<Text style={styles.serviceTitleStyle}> {item.title}</Text>
</View>
);};
What you need is access to "item" of flat list row: ( {index, item} )
answered Nov 7 at 10:58
Louis Solo
1162
1162
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
add a comment |
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Still the error keeps poping up at random times
– Doe
Nov 7 at 11:26
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
Caused by Flat List data source. Please check the random index which you get from "getRandomInt" -> Math.floor(Math.random() * (max - min + 1)) + min.
– Louis Solo
Nov 7 at 11:40
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
just checked ..it has nothing to do with the random nos. I modified the code to return unique nos everytime still i keep getting the same error
– Doe
Nov 8 at 4:50
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
You have only 10 items in "data". Are you sure you always get the index < 10? If the index > 10, data[index] will be undefined.
– Louis Solo
Nov 8 at 5:06
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
silly me.. i got your point and indeed that was the bug. Thnks
– Doe
Nov 8 at 5:29
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187384%2fflatlist-rerendering-issue%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You got two
renderItem
s, did you mean to do that?– Matt Jameson
Nov 7 at 10:21
removed the other one.. was trying sometihng
– Doe
Nov 7 at 10:24