How can I remove an item from a FlatList and then update that list in React Native?












0















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:20}}onPress = {()=>(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.










share|improve this question



























    0















    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:20}}onPress = {()=>(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.










    share|improve this question

























      0












      0








      0








      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:20}}onPress = {()=>(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.










      share|improve this question














      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:20}}onPress = {()=>(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:20}}onPress = {()=>(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:20}}onPress = {()=>(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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 5:23









      Dominic MartireDominic Martire

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          0














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





          share|improve this answer























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


            }
            });














            draft saved

            draft discarded


















            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

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














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





            share|improve this answer




























              0














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





              share|improve this answer


























                0












                0








                0







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





                share|improve this answer













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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 6:57









                Delgee BDelgee B

                665




                665
































                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python