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

86
Vistas
How can I store/setStates of several inputs from a mapped array

So I am working on a project, where a user would provide questions and at the time of applying, the applicant would provide responses to these questions.

Here is my array of the user's questions

const [answers, setAnswers] = useState({})
    questions = [
        {id: 1, questionType: boolean, required: true, question: 'Are you a UK resident?'}, 
        {id: 2, questionType: text, required: true, question: 'give brief description about you'}, 
        {id: 3, questionType: text, required: false, question: 'How old are you'}
     ]

from this array, I've mapped each of them

{questions?.map((q) => (
        <Box sx={{ py: 1 }} key={q.id}>
          <p>{q.question}</p>
          {q?.questionType === 'boolean' ? (
            <Select
              id='location'
              required={q.required}
              onChange={(e) => setAnswers(e.target.value)}
            >
              <MenuItem value='Yes'>Yes</MenuItem>
              <MenuItem value='No'>No</MenuItem>
            </Select>
          ) : (
            <OutlinedInput
              required
              id='name'
              type='text'
              onChange={(e) => setAnswers(e.target.value)}
            />
          )}

The answer of the first one overrides the answer of the second one. How do i fix this?? SO i can persist all the answers.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could for example use a Map which maps question IDs to answers to keep track of questions and the associated answers.

// map that stores an answer to a question ID
// e.g. 
// 1 => Yes
// 2 => I am a developer
// 3 => 123
const [answers, setAnswers] = useState(new Map());
questions = [
  {
    id: 1,
    questionType: boolean,
    required: true,
    question: "Are you a UK resident?",
  },
  {
    id: 2,
    questionType: text,
    required: true,
    question: "give brief description about you",
  },
  { id: 3, questionType: text, required: false, question: "How old are you" },
];

Then have a function which updates the Map whenever a question is answered. This function takes the question ID and the value. It also has to create a new Map every time as React only does a shallow comparison and state should be treated as immutable.

function updateAnswer(id, value) {
  const newMap = new Map(answers);
  newMap.set(id, value);
  setAnswers(newMap);
}

Then last but not least you need to call your update function.

{questions?.map((q) => (
  <Box sx={{ py: 1 }} key={q.id}>
    <p>{q.question}</p>
    {q?.questionType === 'boolean' ? (
      <Select
        id='location'
        required={q.required}
        onChange={(e) => updateAnswer(q.id, e.target.value)}
      >
        <MenuItem value='Yes'>Yes</MenuItem>
        <MenuItem value='No'>No</MenuItem>
      </Select>
    ) : (
      <OutlinedInput
        required
        id='name'
        type='text'
        onChange={(e) => updateAnswer(q.id, e.target.value)}
      />
    )}
about 4 years ago · Juan Pablo Isaza Denunciar
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