Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react async problem: data not displaying after async fetch

hi i am trying to learn react and stuck with this error
error message

could you guys help me my code:

import { useState, useContext } from "react";
import { GameStateContext } from "../helpers/Context"
import { db } from '../../firebase-config'
import { collection, getDocs } from "firebase/firestore";
import { useEffect } from "react";

const Quiz = () => {
 const { score, setScore, gameState, setGameState, selection, setSelection } = useContext(GameStateContext);
 const [isLoading, setLoading] = useState(true);

const questions = [];

useEffect(() => {
    deneme();
}, []);

const deneme = async () => {
    const querySnapshot = await getDocs(collection(db, selection));
    querySnapshot.forEach((doc) => {
        questions.push(doc.data());
    });
    setLoading(false);
}
if (isLoading) {
    return <div> Loading</div>
}
return (
    <div className="Quiz">
        <h1>{questions[currentQuestion].question}</h1>
        <div className="questions">
            <button
                onClick={() => {
                    chooseOption("optionA");
                }}
            >
                {questions[currentQuestion]?.optionA}
            </button>
            <button
                onClick={() => {
                    chooseOption("optionB");
                }}
            >
                {questions[currentQuestion]?.optionB}
            </button>
            <button
                onClick={() => {
                    chooseOption("optionC");
                }}
            >
                {questions[currentQuestion]?.optionC}
            </button>
            <button
                onClick={() => {
                    chooseOption("optionD");
                }}
            >
                {questions[currentQuestion]?.optionD}
            </button>
        </div>

        {currentQuestion == questions.length - 1 ? (
            <button onClick={finishQuiz} id="nextQuestion">
                Finish Quiz
            </button>
        ) : (
            <button onClick={nextQuestion} id="nextQuestion">
                Next Question
            </button>
        )}

    </div>
)

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

inside of deneme function i can access my data but i can not access it below whenever i use "?" error is not showing up but also buttons are not showing up too please help me

example :used ? at: questions[currentQuestion].question

>Solution :

const questions = [];

Putting the data into a local variable named questions is not going to help you. Every render will create a new local variable, and it will be an empty array.

If you want the data to be available on the next render, you need to put it into state:

const Quiz = () => {
  const { score, setScore, gameState, setGameState, selection, setSelection } = useContext(GameStateContext);
  const [isLoading, setLoading] = useState(true);
  const [questions, setquestions] = useState([]);

  useEffect(() => {
    deneme();
  }, []);

  const deneme = async () => {
    const querySnapshot = await getDocs(collection(db, selection));
    const newQuestions = querySnapshot.docs.map(doc => doc.data());
    setQuestions(newQuestions);
    setLoading(false);
  }

  //....
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading