Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Assigning a value to a list of items

I am trying to update each answer in a list so I can pass each answer to redux and send all the answers to an API.

I have tried to pass each answer as a variable and assign that to a state in an array. I am really confused by this.

const DATA = [{
    id: '1',
    title: 'Do you feel appropriately rewarded for your work?',
  },
  {
    id: '2',
    title: 'Are you happy with your working hours?',
  },
  {
    id: '3',
    title: 'Do you feel recognised when you do something well?',
  },
  {
    id: '4',
    title: 'Do you have enough information to do your job well?',
  },
  {
    id: '5',
    title: 'Do you have the resources you need to do your job well?',
  },
  {
    id: '6',
    title: 'Do you feel empowered to make decisions?',
  },

];

const [data, setData] = useState('');
let newData = [...data];
const questionIndex = newData.findIndex((q, i) => q.id == idVariable);
if (questionIndex > -1) newData[questionIndex].answer = answerVariable;


const handleSubmit = () => {
  navigation.navigate('SplashScreen')
  setData(newData);
  console.log("hey", newData)
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

import React, { useEffect, useState } from "react";

const data = [{
    id: '1',
    title: 'Do you feel appropriately rewarded for your work?',
  },
  {
    id: '2',
    title: 'Are you happy with your working hours?',
  },
  {
    id: '3',
    title: 'Do you feel recognised when you do something well?',
  },
  {
    id: '4',
    title: 'Do you have enough information to do your job well?',
  },
  {
    id: '5',
    title: 'Do you have the resources you need to do your job well?',
  },
  {
    id: '6',
    title: 'Do you feel empowered to make decisions?',
  },

];
about 4 years ago · Juan Pablo Isaza Relatório

0

it should be like that, pelease try, as setState udate asych way to you will not get result immidieatly

const [questions, setQuestions] = useState(DATA)

const handleSubmit = () => { 
  setQuestions((prevState) => {
       const questionIndex =  prevState.findIndex((question) => question.id === idVariable)
      if (questionIndex > -1) {
         questions[questionIndex].answer = answerVariable;
      }

    return questions;

  });
  navigation.navigate('SplashScreen')
  console.log("hey", newData)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda