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').











share|improve this question
























  • You got two renderItems, 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















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').











share|improve this question
























  • You got two renderItems, 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













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').











share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 22:36









halfer

14.2k757105




14.2k757105










asked Nov 7 at 10:14









Doe

14




14












  • You got two renderItems, 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 renderItems, 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 renderItems, did you mean to do that?
– Matt Jameson
Nov 7 at 10:21




You got two renderItems, 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












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>
);
};





share|improve this answer





















  • removed the extra renderItem it still gives error
    – Doe
    Nov 7 at 10:45


















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} )






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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>
);
};





share|improve this answer





















  • removed the extra renderItem it still gives error
    – Doe
    Nov 7 at 10:45















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>
);
};





share|improve this answer





















  • removed the extra renderItem it still gives error
    – Doe
    Nov 7 at 10:45













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>
);
};





share|improve this answer












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>
);
};






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 10:22









jonixj

387311




387311












  • 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




removed the extra renderItem it still gives error
– Doe
Nov 7 at 10:45












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} )






share|improve this answer





















  • 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















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} )






share|improve this answer





















  • 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













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} )






share|improve this answer












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} )







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini