How can I remove an item from a FlatList and then update that list in React Native?
I am making a To Do list app using React Native, where I add events to a FlatList and then have a button that removes that event once it it finished. So far this is what I have. It seems very hacky to me, but most of it works.
import React from 'react';
import StyleSheet, Text, View, TextInput,TouchableOpacity, FlatList from 'react-native';
export default class App extends React.Component
constructor(props)
const data = ;
super(props);
this.state =
text: 'Enter activity here',
data: data,
color: true,
currNum: 0,
updateText()
this.setState(data:this.state.data.concat(key:this.state.text,index:this.state.currNum));
this.state.currNum++;
removeText(item)
this.setState(data:this.state.data.pop(item.index));
this.state.currNum--;
render()
return (
<View style=styles.container>
<Text></Text>
<View style = flexDirection:'row',justifyContent:'flex-end'>
<TextInput style = fontSize:30,borderColor:'black', flex:1, marginTop:20 onChangeText = (text) => this.setState(text)value = this.state.text/>
<TouchableOpacity style = marginTop:20onPress = ()=>(this.updateText())>
<Text>Add to list</Text>
</TouchableOpacity>
</View>
<View style = flex:1, flexDirection:'row'>
<FlatList
data = this.state.data
extraData = this.state
renderItem = (item) => <View><Text style=styles.text >item.key</Text><TouchableOpacity onPress = () => this.removeText(item)><Text>Remove</Text></TouchableOpacity></View>
/>
</View>
</View>
);
When I press the "remove" button, I delete an element from the list of data that the FlatList uses. However, whenever I do this I get an error saying "Tried to get frame for out of range index NaN". Is there a way for me to regularly update and remove a FlatList, and to re-render the FlatList once I have removed an item? I have tried using the extraDate prop, but it hasn't worked. I believe I am using it wrong though. Thank you for all the help.
javascript react-native
add a comment |
I am making a To Do list app using React Native, where I add events to a FlatList and then have a button that removes that event once it it finished. So far this is what I have. It seems very hacky to me, but most of it works.
import React from 'react';
import StyleSheet, Text, View, TextInput,TouchableOpacity, FlatList from 'react-native';
export default class App extends React.Component
constructor(props)
const data = ;
super(props);
this.state =
text: 'Enter activity here',
data: data,
color: true,
currNum: 0,
updateText()
this.setState(data:this.state.data.concat(key:this.state.text,index:this.state.currNum));
this.state.currNum++;
removeText(item)
this.setState(data:this.state.data.pop(item.index));
this.state.currNum--;
render()
return (
<View style=styles.container>
<Text></Text>
<View style = flexDirection:'row',justifyContent:'flex-end'>
<TextInput style = fontSize:30,borderColor:'black', flex:1, marginTop:20 onChangeText = (text) => this.setState(text)value = this.state.text/>
<TouchableOpacity style = marginTop:20onPress = ()=>(this.updateText())>
<Text>Add to list</Text>
</TouchableOpacity>
</View>
<View style = flex:1, flexDirection:'row'>
<FlatList
data = this.state.data
extraData = this.state
renderItem = (item) => <View><Text style=styles.text >item.key</Text><TouchableOpacity onPress = () => this.removeText(item)><Text>Remove</Text></TouchableOpacity></View>
/>
</View>
</View>
);
When I press the "remove" button, I delete an element from the list of data that the FlatList uses. However, whenever I do this I get an error saying "Tried to get frame for out of range index NaN". Is there a way for me to regularly update and remove a FlatList, and to re-render the FlatList once I have removed an item? I have tried using the extraDate prop, but it hasn't worked. I believe I am using it wrong though. Thank you for all the help.
javascript react-native
add a comment |
I am making a To Do list app using React Native, where I add events to a FlatList and then have a button that removes that event once it it finished. So far this is what I have. It seems very hacky to me, but most of it works.
import React from 'react';
import StyleSheet, Text, View, TextInput,TouchableOpacity, FlatList from 'react-native';
export default class App extends React.Component
constructor(props)
const data = ;
super(props);
this.state =
text: 'Enter activity here',
data: data,
color: true,
currNum: 0,
updateText()
this.setState(data:this.state.data.concat(key:this.state.text,index:this.state.currNum));
this.state.currNum++;
removeText(item)
this.setState(data:this.state.data.pop(item.index));
this.state.currNum--;
render()
return (
<View style=styles.container>
<Text></Text>
<View style = flexDirection:'row',justifyContent:'flex-end'>
<TextInput style = fontSize:30,borderColor:'black', flex:1, marginTop:20 onChangeText = (text) => this.setState(text)value = this.state.text/>
<TouchableOpacity style = marginTop:20onPress = ()=>(this.updateText())>
<Text>Add to list</Text>
</TouchableOpacity>
</View>
<View style = flex:1, flexDirection:'row'>
<FlatList
data = this.state.data
extraData = this.state
renderItem = (item) => <View><Text style=styles.text >item.key</Text><TouchableOpacity onPress = () => this.removeText(item)><Text>Remove</Text></TouchableOpacity></View>
/>
</View>
</View>
);
When I press the "remove" button, I delete an element from the list of data that the FlatList uses. However, whenever I do this I get an error saying "Tried to get frame for out of range index NaN". Is there a way for me to regularly update and remove a FlatList, and to re-render the FlatList once I have removed an item? I have tried using the extraDate prop, but it hasn't worked. I believe I am using it wrong though. Thank you for all the help.
javascript react-native
I am making a To Do list app using React Native, where I add events to a FlatList and then have a button that removes that event once it it finished. So far this is what I have. It seems very hacky to me, but most of it works.
import React from 'react';
import StyleSheet, Text, View, TextInput,TouchableOpacity, FlatList from 'react-native';
export default class App extends React.Component
constructor(props)
const data = ;
super(props);
this.state =
text: 'Enter activity here',
data: data,
color: true,
currNum: 0,
updateText()
this.setState(data:this.state.data.concat(key:this.state.text,index:this.state.currNum));
this.state.currNum++;
removeText(item)
this.setState(data:this.state.data.pop(item.index));
this.state.currNum--;
render()
return (
<View style=styles.container>
<Text></Text>
<View style = flexDirection:'row',justifyContent:'flex-end'>
<TextInput style = fontSize:30,borderColor:'black', flex:1, marginTop:20 onChangeText = (text) => this.setState(text)value = this.state.text/>
<TouchableOpacity style = marginTop:20onPress = ()=>(this.updateText())>
<Text>Add to list</Text>
</TouchableOpacity>
</View>
<View style = flex:1, flexDirection:'row'>
<FlatList
data = this.state.data
extraData = this.state
renderItem = (item) => <View><Text style=styles.text >item.key</Text><TouchableOpacity onPress = () => this.removeText(item)><Text>Remove</Text></TouchableOpacity></View>
/>
</View>
</View>
);
When I press the "remove" button, I delete an element from the list of data that the FlatList uses. However, whenever I do this I get an error saying "Tried to get frame for out of range index NaN". Is there a way for me to regularly update and remove a FlatList, and to re-render the FlatList once I have removed an item? I have tried using the extraDate prop, but it hasn't worked. I believe I am using it wrong though. Thank you for all the help.
import React from 'react';
import StyleSheet, Text, View, TextInput,TouchableOpacity, FlatList from 'react-native';
export default class App extends React.Component
constructor(props)
const data = ;
super(props);
this.state =
text: 'Enter activity here',
data: data,
color: true,
currNum: 0,
updateText()
this.setState(data:this.state.data.concat(key:this.state.text,index:this.state.currNum));
this.state.currNum++;
removeText(item)
this.setState(data:this.state.data.pop(item.index));
this.state.currNum--;
render()
return (
<View style=styles.container>
<Text></Text>
<View style = flexDirection:'row',justifyContent:'flex-end'>
<TextInput style = fontSize:30,borderColor:'black', flex:1, marginTop:20 onChangeText = (text) => this.setState(text)value = this.state.text/>
<TouchableOpacity style = marginTop:20onPress = ()=>(this.updateText())>
<Text>Add to list</Text>
</TouchableOpacity>
</View>
<View style = flex:1, flexDirection:'row'>
<FlatList
data = this.state.data
extraData = this.state
renderItem = (item) => <View><Text style=styles.text >item.key</Text><TouchableOpacity onPress = () => this.removeText(item)><Text>Remove</Text></TouchableOpacity></View>
/>
</View>
</View>
);
import React from 'react';
import StyleSheet, Text, View, TextInput,TouchableOpacity, FlatList from 'react-native';
export default class App extends React.Component
constructor(props)
const data = ;
super(props);
this.state =
text: 'Enter activity here',
data: data,
color: true,
currNum: 0,
updateText()
this.setState(data:this.state.data.concat(key:this.state.text,index:this.state.currNum));
this.state.currNum++;
removeText(item)
this.setState(data:this.state.data.pop(item.index));
this.state.currNum--;
render()
return (
<View style=styles.container>
<Text></Text>
<View style = flexDirection:'row',justifyContent:'flex-end'>
<TextInput style = fontSize:30,borderColor:'black', flex:1, marginTop:20 onChangeText = (text) => this.setState(text)value = this.state.text/>
<TouchableOpacity style = marginTop:20onPress = ()=>(this.updateText())>
<Text>Add to list</Text>
</TouchableOpacity>
</View>
<View style = flex:1, flexDirection:'row'>
<FlatList
data = this.state.data
extraData = this.state
renderItem = (item) => <View><Text style=styles.text >item.key</Text><TouchableOpacity onPress = () => this.removeText(item)><Text>Remove</Text></TouchableOpacity></View>
/>
</View>
</View>
);
javascript react-native
javascript react-native
asked Nov 15 '18 at 5:23
Dominic MartireDominic Martire
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
use this instead you shouldn't mutate this.state and Array.prototype.pop() mutates it
removeText(item, index)
this.setState(data: [
...this.state.data.slice(0, index),
...this.state.data.slice(index + 1)
]);
this.state.currNum--;
add a comment |
I think you should use .filter
removeText(item)
this.setState(
data: this.state.data.filter((_item)=>_item.key !== item.key)
);
And what this.state.currNum
- is for?
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53312934%2fhow-can-i-remove-an-item-from-a-flatlist-and-then-update-that-list-in-react-nati%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
use this instead you shouldn't mutate this.state and Array.prototype.pop() mutates it
removeText(item, index)
this.setState(data: [
...this.state.data.slice(0, index),
...this.state.data.slice(index + 1)
]);
this.state.currNum--;
add a comment |
use this instead you shouldn't mutate this.state and Array.prototype.pop() mutates it
removeText(item, index)
this.setState(data: [
...this.state.data.slice(0, index),
...this.state.data.slice(index + 1)
]);
this.state.currNum--;
add a comment |
use this instead you shouldn't mutate this.state and Array.prototype.pop() mutates it
removeText(item, index)
this.setState(data: [
...this.state.data.slice(0, index),
...this.state.data.slice(index + 1)
]);
this.state.currNum--;
use this instead you shouldn't mutate this.state and Array.prototype.pop() mutates it
removeText(item, index)
this.setState(data: [
...this.state.data.slice(0, index),
...this.state.data.slice(index + 1)
]);
this.state.currNum--;
answered Nov 15 '18 at 6:57
Delgee BDelgee B
16616
16616
add a comment |
add a comment |
I think you should use .filter
removeText(item)
this.setState(
data: this.state.data.filter((_item)=>_item.key !== item.key)
);
And what this.state.currNum
- is for?
add a comment |
I think you should use .filter
removeText(item)
this.setState(
data: this.state.data.filter((_item)=>_item.key !== item.key)
);
And what this.state.currNum
- is for?
add a comment |
I think you should use .filter
removeText(item)
this.setState(
data: this.state.data.filter((_item)=>_item.key !== item.key)
);
And what this.state.currNum
- is for?
I think you should use .filter
removeText(item)
this.setState(
data: this.state.data.filter((_item)=>_item.key !== item.key)
);
And what this.state.currNum
- is for?
edited Mar 11 at 10:28
chiril.sarajiu
9861016
9861016
answered Mar 11 at 8:35
Дмитрий ЛДмитрий Л
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53312934%2fhow-can-i-remove-an-item-from-a-flatlist-and-then-update-that-list-in-react-nati%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