Respond to components that do not update when updating the database

I set up a tracker for my smart home dashboard (built-in response and firebase). However, when the firebase database changes, it does not cause redrawing on the reacting component. I can’t say where I’m wrong, can someone help me?

import React from 'react';
import * as firebase from 'firebase';
import moment from 'moment';

export default class IO extends React.Component {
  constructor() {
    super();
    this.state = {
      people: []
    };
  }

  componentDidMount() {
    let peopleArray = []
    this.dbroot = firebase.database().ref().child('io')
    this.dbroot.on('value', snapshot => {
      snapshot.forEach((snap) => {
        if(snap.val().active === true) {
          peopleArray.push(snap.val())
        }
      })
      this.setState({people: peopleArray})
    });
  }

  render() {
    return(
      <section class="c-module c-module_IO">
        <ul>
          {
            this.state.people.map((p) => {
              return <li key={ p.name } class={'status--'+p.status}><img src={p.image} alt={ p.name } /></li>
            })
          }
        </ul>
      </section>
    )
  }
}
+4
source share
1 answer

, ComponentDidMount peopleArray. , , Firebase. , peopleArray , . React , , - , . , . .

, let peopleArray = [] onbalue firebase, , . , Firebase , . , , .

CDM:

componentDidMount() {
  this.dbroot = firebase.database().ref().child('io')
  this.dbroot.on('value', snapshot => {
    let peopleArray = []
    snapshot.forEach((snap) => {
      if(snap.val().active === true) {
        peopleArray.push(snap.val())
      }
    })
    this.setState({people: peopleArray})
  });
}
+4

Source: https://habr.com/ru/post/1671874/


All Articles