Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
How do I save the state from different child components into a single state array in the parent component with all the values

Im trying to save the states from the child components into an array in the parent component. The structure is somewhat like this : -


<App>
<Quiz />
<Quiz />
<Quiz />
<Quiz />
<Quiz />
</App>

Each Quiz child component has a state in it called selectedOption which contains the value of the option selected(button clicked) . Is there a way to pass all these into into a single state array in the parent <App /> component ? Im trying to save them in an array so that I can map over its values later and check if its equal to the respective option in another array containing the correct answers (with the help of index).

Code

<App / >

function App() {
  const [showMenu, changeMenu] = useState(true);
  const [correctAnswers, setCorrectAnswers] = useState(["", "", "", "", ""]) // i need to save them here
  const [quiz, setQuiz] = useState([]);

  function getQuiz() {
    fetch("my api key")
      .then((res) => res.json())
      .then((data) => setQuiz(data["results"]));
  }

  function getValues(val) {
    console.log(val);
  //this was only for the purpose of debugging but didnt work as i wanted
  }

  React.useEffect(() => {
    getQuiz();
  }, []);

  const renderQuiz = quiz.map((val) => {
    let question = decodeURIComponent(val["question"]);
    let correctAnswer = decodeURIComponent(val["correct_answer"]);
    let wrongOptions = val["incorrect_answers"];
    let allOptions = [];
    wrongOptions.map((elem) => allOptions.push(decodeURIComponent(elem)));
    allOptions.push(correctAnswer);
    allOptions = shuffle(allOptions);

    return (
      <Quiz
        question={question}
        options={allOptions}
        correctOption={correctAnswer}
        getValues={getValues}
      />
    );
  });

}

A portion of the <Quiz/> component

< Quiz / >

export default function Quiz({
  options,
  question,
  getValues,
  correctOption,
  ...props
}) {
  const [chosenOne, setChosenOne] = React.useState();
  const [selectedOption, setSelectedOption] = useState("");
  const [isCorrect, setisCorrect] = useState(false);
  const list = [...options]; 

  function simplyPrintThis(x) {
    let val = x.value;
    console.log(val);
    setSelectedOption(val);
    setisCorrect(val == correctOption)
  }


about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda