Not able to listen for data changes from API

Viewed 109

I have used firebase for API in my React app, where it listens for the user data. When users data is updated, I am not able to listen for data changes in my React component. How to implement this functionality.

    componentDidMount() {
        axios.get('https://organic-kart-3f887.firebaseio.com/users.json')
            .then(users => {
                this.setState({
                    users.data
                })
            })
    }

I am getting the user data, when the component is mounted. I am not able to listen for changes in users data in my app.

1 Answers

I would suggest you change the exact data will be changed instead of directly changing the high-level use object itself. Please try the below approach.

componentDidMount() {
        axios.get('https://organic-kart-3f887.firebaseio.com/users.json')
            .then(users => {
                this.setState({
                    users: {
                    ...this.state.users,
                    data

                })
            })
    }
Related