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

188
Visualizações
How to iterate through select dropdown in react using props passed from Parent Component?

I am using props from a parent component in a modal and I am unsure of how to make it show in a select dropdown dynamically. I see that the stubbed hardcoded 'Agent' value is being repeated but how do I actually make that dynamically show the values from the props I am receiving? What am I missing here?

React Modal Component

import React, { useState, useEffect } from 'react'
import Button from '@/components/Button.jsx'
import Loading from '@/components/Loading.jsx'
import Modal from '@/components/Modal.jsx'

/**
 * Handles displaying the capabilities modal for creating new capabilities or updating exiting ones.
 * @component
 */ 
export default function AddCapabilitiesModal({ profile, onClose }) {
  const [error, setError] = useState(null)
  const [loading, setLoading] = useState(false)
  const [agents, setAgents] = useState([])
  const [profiles, setProfiles] = useState([])

  useEffect(() => {
    if (profile) {
      console.log(profile);
      setAgents(profile.map((f) => ({ label: f.agent, value: f.agent })))
      setLoading(false);
    }
      }, [])

    /**
   * Erases information of text field variables and closes modal.
   */
     const closeModal = () => {
      setError(false)
      setLoading(false)
      onClose()
    }

    const dummyClick = () => {
      console.log('click')
    }

  return (
    <div>
    <Modal>
        {error && (
          <label className='high'>
            Error: Please confirm inputs and try again.
          </label>
        )}
        <div className='modal-title'>
          <label>EDIT STATUS</label>
        </div>
        <br />
        <Loading text='Saving Test...' visible={loading} />
        {!loading && (
        <div id='addCapSelectInput' className='sub-filters'>
          <div className='lrow filter-options group light-grey-bg'>

          <select>
          {agents.map((agent) => (
            <option key={agent} value={agent}>
            Agent
          </option>
            ))} 
          </select>

          </div>
        </div>
        )}
        <div className='modal-row top-horizontal-divider'>
          <Button onClick={closeModal} className='modal-cancel'>
            Cancel
          </Button>
          <Button
            onClick={dummyClick}
            className='modal-submit'
          >
            Submit
          </Button>
        </div>
      </Modal>
    </div>
  )
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As I can see your agent in state is a list of objects containing label and value keys. So you should just rewrite render part to something like this:

{
     agents.map((agent) => (
       <option key={agent.label} value={agent.value}>
         {agent.label}
        </option>
     ))
   }
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