title: Delete Item by Key, Firebase React Native
body: <p>I have a simple Notes React Native app and I am able to add and get data to it, but I am not sure how to remove/update data. The main problem is in getting the part where I tell firebase which data to remove. How can I pass a firebase key to a 'delete' function that takes the key as parameter and remove it from firebase.
  I'm an absolute beginner at React Native, my code is the following:</p>
  
  <pre><code>export default class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
      all_notitas: [],
      notita_text: ''
    };
  };
  
  componentWillMount() {
    const notitasRef = firebase.database().ref('notitas');
    this.listenForNotitas(notitasRef);
  };
  
  listenForNotitas = (notitasRef) =&gt; {
    notitasRef.on('value', (dataSnapshot) =&gt; {
      var aux = [];
      dataSnapshot.forEach((child) =&gt; {
        aux.push({
          date: child.val().date,
          notita: child.val().notita
        });
      });
      this.setState({all_notitas: aux});
    });
  };  // listenForNotitas
  
  render() {
    let show_notitas = this.state.all_notitas.map((val, key) =&gt; {
      return (
        &lt;Notita
          key={key}
          keyval={key}
          val={val}
          eventDeleteNotita={()=&gt;this.deleteNotita(key)}&gt; // I THINK THIS IS THE PROBLEM
        &lt;/Notita&gt;
      );
    });
  
    return (
      &lt;View style={styles.container}&gt;
  
        &lt;View style={styles.header}&gt;
          &lt;Text style={styles.headerText}&gt;NOTITAS&lt;/Text&gt;
        &lt;/View&gt;
  
        &lt;ScrollView style={styles.scrollContainer}&gt;
          {show_notitas}
        &lt;/ScrollView&gt;
  
        &lt;View style={styles.footer}&gt;
          &lt;TouchableOpacity
            style={styles.addButton}
            onPress={this.addNotita.bind(this)}&gt;
            &lt;Text style={styles.addButtonText}&gt;+&lt;/Text&gt;
          &lt;/TouchableOpacity&gt;
  
          &lt;TextInput
            style={styles.textInput}
            placeholder='&gt;&gt;&gt; Escribir notita'
            placeholderTextColor='white'
            underlineColorAndroid='transparent'
            onChangeText={(notita_text) =&gt; (this.setState({notita_text}))}
            value={this.state.notita_text}&gt;
  
          &lt;/TextInput&gt;
        &lt;/View&gt;
  
      &lt;/View&gt;
    );
  }
  
  addNotita() {
    if (this.state.notita_text) {
      var d = new Date();
      dataForPush = {
        'date': d.getDate() + '-' +  d.getMonth() + '-' + d.getFullYear(),
        'notita': this.state.notita_text
      };
      firebase.database().ref('notitas').push(dataForPush);
      this.state.all_notitas.push(dataForPush);
      this.setState(
        {
          all_notitas: this.state.all_notitas,
          notita_text: '',  // Limpiar input
        }
      )
    }  // end if
  }  // addNotita
  </code></pre>
  
  <p>When I do 'console.log(key)', it returns an int like 0, 1, 2, etc. It should return a firebase key like '-LRtghw8CjMsftSAXMUg' for example. I don't know what I am doing wrong and how to fix it.</p>
  
  <pre><code>deleteNotita(key) {
    firebase.database().ref('notitas').child('' + key).remove()
  
    /*
    let updates = {};
    console.log(key);
    console.log(updates['/notitas/' + key]);
    updates['/notitas/' + key] = null;
    firebase.database().ref().update(updates); */
  
    this.state.all_notitas.splice(key, 1);
    this.setState({all_notitas: this.state.all_notitas});
  } // deleteNotita
  
  }
  </code></pre>