Home > Mobile >  React native firestore statics page querries
React native firestore statics page querries

Time:08-06

I just want to make datatable for reporting. but i cant get counts that i want.

i got city ids and theres data related on cities

i need to make datatable which one get counts about cities. i can get numbers on console.log but cant show on screen. how can i do that? i tried things below i did GetDetails return snap.size didnt worked i tried flatlist but didnt work i tried call data with function but didnt work

import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, Button, ScrollView, FlatList } from 'react-native'
import firestore from '@react-native-firebase/firestore';
import { DataTable } from 'react-native-paper';


export default function TargetStaticsScreen() {

    const [zones, setZones] = useState([])

    function GetCities() {
        var z = [];
        firestore().collection('RandomZones').orderBy('City', 'asc').get().then(zones => {
            setZones(zones.docs)
        })

    }

    async function GetDetails({ cityid }) {
        let snap = await firestore().collection('RandomZones').doc(cityid).collection('targetlist').get()
        return (<DataTable.Cell key={cityid} numeric>{snap.size}</DataTable.Cell>
        )

    }
    function RemainingDots(cityid) {
        firestore().collection('RandomZones').doc(cityid).collection('targetlist').where('isUsed', '==', false).get().then(snap => {
            return snap.size;
        })
    }


    return (
        <ScrollView>
            <Button title='getdata' onPress={() => GetCities()} />
            <DataTable>
                <DataTable.Header>
                    <DataTable.Title>İl</DataTable.Title>
                    <DataTable.Title numeric>Toplam Nokta</DataTable.Title>
                    <DataTable.Title numeric>Kalan Nokta</DataTable.Title>
                    <DataTable.Title numeric>Aktif Nokta</DataTable.Title>
                    <DataTable.Title numeric>Aktif Nokta Tutar</DataTable.Title>
                </DataTable.Header>

                {zones.map((x) => {

                    return (
                        <DataTable.Row key={x.id}>
                            <DataTable.Cell>{x.id}</DataTable.Cell>
                            <GetDetails cityid={x.id} />
                            <DataTable.Cell numeric>{RemainingDots(x.id)}</DataTable.Cell>
                            <DataTable.Cell numeric>111</DataTable.Cell>
                            <DataTable.Cell numeric>111</DataTable.Cell>
                        </DataTable.Row>
                    )
                }
                )}

            </DataTable>
        </ScrollView>);
}

CodePudding user response:

Since the value you're looking for is loaded asynchronously from the database, you'll have to put it in a state variable to get the UI to update - very similarly to what you already do for setZones.

const [detailsForRegions, setDetailsForRegions] = useState({})

...

async function GetDetails(cityid) {
  firestore().collection('RandomZones').doc(cityid).collection('targetlist').get().then((snap) => {
    let data = detailsForRegions;
    data[cityid] = snap.size;
    setDetailsForRegions(data);
  })
}

You should probably also trigger the loading of this data from the then of GetCities, rather than the UI rendering:

function GetCities() {
    var z = [];
    firestore().collection('RandomZones').orderBy('City', 'asc').get().then(zones => {
        setZones(zones.docs)
        zones.docs.map(zone => GetDetails(zone.id)); //            
  • Related