-1

I am struggling with the update of a list when deleting a given user(s). Most of the problem comes from the fact that I lose the state because I place the function in the componentDidMount as a setParams.

componentDidMount() {
    ...
    this.props.navigation.setParams({ removeUser: this._deleteUser});
    ...
}

And this is the function of _deleteUsers which deletes fine but just does not update:

_deleteUser(){
    var user = firebase.auth().currentUser;
    var position;
    _.map(this.selectedMembers, item => {
        var ref = firebase.database().ref('path')
        ref.orderByChild('key').equalTo(item.key).on("value", function(snapshot){
            snapshot.forEach(function(child) {
                position = child.key
            });
        });
        delete this.students.members[position]
    });

    firebase.database().ref().child('path').remove(); 

    Toast.show({
        text: "Members deleted successfully!",
        type: "success",
        fontStyle: "italic", 
    })        
}

All this should be rendered in the following Flatlist

    render() {
    ...
                <FlatList 
                        style={{
                            flex: 1,
                            width: "100%",
                        }}
                        refreshControl={
                            <RefreshControl
                                refreshing={this.state.refreshing}
                                onRefresh={this.refreshList}
                            />}
                        data={this.state.dataSource}
                        ItemSeparatorComponent={this.FlatListItemSeparator}
                        renderItem = {this._renderItem}
                        listKey={item => item.uid}
                        extraData={this.state}
                 />
}

and the state of the application is:

constructor(props) {
   super(props)
   this.state = {
            dataSource : [],
            text : '',
            itemNumber : 0,
            selectedItems: [],           
            groupName: '',
            selectedMembers: [],
        }
    }  
    state = {
        refreshing: false,
    }
    refreshList = () => {
        this.state.refreshing = true;
        this.props.navigation.state.params.store = new StudentsStore();
        this.state.refreshing = false;
    }

How should I approach it? I have many other operations but the only one that does not work is this one because of the way I approach it in the componentDidMount or at least, this is what I think it is the issue I don't know how to solve.

Thanks,

user123
  • 175
  • 4
  • 16

1 Answers1

1

You are losing your context when passing the callback. Wrap your callback in an arrow function, and you will keep the original context.

Change:

componentDidMount() {
    ...
    this.props.navigation.setParams({ removeUser: this._deleteUser});
    ...
}

for this:

componentDidMount() {
    ...
    this.props.navigation.setParams({ removeUser: () => this._deleteUser()});
    ...
}
Reynau
  • 194
  • 9