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

165
Visualizações
Mapping to React Modal renders all items from array instead of mapping by their ID

My issue is that when I map the data into the Modal, it is returning all items from that field in the array e.g. mapping Business Name will return all business names in the array instead of 1.

I am also using React-Bootstrap-Table-Next in the same component to render data. A button is added to each row of the table which I would like to click and bring up more information on that particular row.

Button being rendered in the table with onClick handler to open the modal:

const columns = [
        { dataField: "View", text: "View", 
        formatter: () => {return <Button variant="success" onClick={handleShow}>View</Button>},
        headerStyle: () => {return { width: "5%" };} },
        { dataField: "BusinessName", text: "Business Name", headerStyle: () => {return { width: "27%" };} },
    ];

Modal state handling:

    const [showModal, setShow] = useState(false);
    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

Modal code with map function:

<Modal 
            show={showModal} 
            onHide={handleClose}
            size="lg" 
            centered
        >
            <Modal.Header closeButton>
                {submitted.map((item) => 
                <Modal.Title key={item.Id}> {item.BusinessName}</Modal.Title>
                )}  
            </Modal.Header>
            <Modal.Body></Modal.Body>
            <Modal.Footer>
                <Button variant="secondary" onClick={handleClose}>
                    Close
                </Button>
                <Button variant="primary" onClick={handleClose}>
                    Save Changes
                </Button>
            </Modal.Footer>
        </Modal>

Getting and setting the data:

const [submitted, setSubmitted] = useState([]);


const getSubmittedData = async () => {
        try {
            const response = await Axios.get(url);
            setSubmitted(response.data);
            console.log(response.data);
            setLoading(true);
        } catch (e) {
            console.log(e)
        }
    };

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

0

It's a little unclear what the expected result should be and what is contained in the submitted array.

Map will create a new array with all the returned values from the given function. If you're only wanting to render some values then maybe you need to filter first.

If you just want the items that include an Id then maybe something like

{submitted.filter(item => item.id).map(item => (
//render items here
)}

I don't have enough rep to comment to clarify information, so hope this is useful. Regards

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