Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

reactjs - react native swr for firebase got error Rendered more hooks than during the previous render

I use swr firestore to get data from firestore:

this is my code:

import { Divider } from '@ui-kitten/components';
import React, { useEffect, useState } from 'react';
import { ScrollView, StyleSheet, Text } from 'react-native';
import { useCollection } from '@nandorojo/swr-firestore'
import { NavigationBar } from '../common';
import { SafeAreaView } from 'react-native-safe-area-context';
import { GetCurrentMerchantId } from '../../data/storage';

import { FoodMenuListTemplate } from "../templates";

export default function FoodMenuSelector({ navigation, route }) {
    const [merchantId, setMerchantId] = useState('');
    const [data, setData] = useState([]);
    const { data: dataFromServer, error } = useCollection(`FoodMenus`, {
        where: ['merchantId', '==', merchantId],
        listen: true
    })


    if (error) return <Text>Error!</Text>
    if (!dataFromServer) return <Text>Loading...</Text>

    useEffect(() => {
        const setMerchantById = async () => {
            const currentMerchantId = await GetCurrentMerchantId();
            setMerchantId(currentMerchantId);
        }
        setMerchantById();
    }, []);

    setData(dataFromServer);



    const onItemPress = (selectedItem) => {
        selectedItem.selected = !selectedItem.selected;
        const filtered = data.filter(item => item.id != selectedItem.id);
        filtered.push(selectedItem);
        setData(filtered);
    }

    return (
        <SafeAreaView
            insets='top'>
            <NavigationBar
                title={'Food Menu'}
                subtitle={'Select food menu'}
                hasBack={true}
                onSave={() => {
                    route.params.onMenusSelected(data.filter(item => item.selected));
                    navigation.goBack();
                }} />
            <Divider />
            <ScrollView style={styles.container}>
                {data.length == 0 ?
                    <Text style={{ textAlign: 'center', marginTop: 50 }}>No Food Menu Found, Create one ...</Text>
                    : <FoodMenuListTemplate data={data} onItemPress={onItemPress} />}
            </ScrollView>
        </SafeAreaView>
    )
}

const styles = StyleSheet.create({
    container: {
        margin: 10
    }
});

but I got error: Rendered more hooks than during the previous render

The idea is I want to get data from firestore use swr firestore and set that data to setData so I can manipulate data if there are any event and change.

but before that, I must get current merchant from asyncstorage.

how to solve this? I still confuse how to use hooks properly

question from:https://stackoverflow.com/questions/66062223/react-native-swr-for-firebase-got-error-rendered-more-hooks-than-during-the-prev

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...