0

I Have Code Like this Model.js

    import firebase from '../config/Firebase'
    class Dashboard {
        GetAllBlog() {
            return firebase.collection('blogs').get()
        }
    }
    export default new Dashboard()

then, I return model to home.js

import React from 'react';
import Index from './components/index'
import CardOverview from './components/CardOverview'

import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container'
import Paper from '@material-ui/core/Paper'
import Grid from '@material-ui/core/Grid'
import Typography from '@material-ui/core/Typography';
import DashboardService from '../services/dashboard'
const styles = theme => ({
  root: {
    padding: theme.spacing(3, 2),
  },
  row: {
    padding: theme.spacing(3, 2),
  }
});

class Home extends React.Component {
  state = {
    blogs: []
  }

  componentDidMount() {
    DashboardService.GetAllBlog()
      .then(snapshot => {
        this.setState({
          blogs: snapshot
        })
      })
      .catch(err => {
        console.log('Error getting documents', err);
      });
  }
  render() {
    const { classes } = this.props
    const {
      blogs
    } = this.state

    blogs.forEach(element => {
      console.log("blogs", element.data().title)

    });
    return (
.......

but I Got Double when using foreach like this

enter image description here .,

Actually, I have data like this(Two data)

enter image description here

I Look how to make it simple with Convert All snapshot to Array,(I can create joined array from firebase), but I confused, Why this double loop, but it loop 2 times, but in one time, its loop based by length

1 Answer 1

1

this is because the state was changed 2 times, then the render will executed every time you change the state

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.