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

192
Vistas
react get data and render it in a modal

I'm trying to get data of a table to modal, I got a table and all data are populated just fine, I got a button next to each row where I will like that when I click the button I display all data from the selected row. I got to manage to get the data with console.log, but I cannot get it to render in the modal. I got no errors but still it doesn't show either. I've attached an image with what i got. as you in the console. I get the select row

how to make data show in the modal

customsite.jsx

 import React, { useState } from 'react';
 import Modal from './reusable/modal';
 import useFetchData from './hooks/useFetchData';

  const Customsite = ()=> {

const {
    data,
    loading,
  } = useFetchData();

 
const [display, openModal] = useState(false);
   
 const closeModal = () =>{
    openModal(false);
}


const openedModal = () =>{
  openModal(true);
}

   
const getAllData = (data) =>{

      console.log(data);
      return data;
 


     }
return(


    <div>
      
 <div className='conatiner'>

     <table className="table">
  <thead>
   <tr>
  <th>id</th>
  <th>titel</th>
  <th>body</th>
  <th>actions</th>
  <th>details</th>
</tr>
   </thead>
<tbody>
      {data.map(posts =>(

  <tr key={posts.id}>
  <th>{posts.id}</th>
  <th>{posts.title}</th>
  <th>{posts.body}</th>
  <th><button className="btn btn-primary"
   onClick={()=> { getAllData(posts); openedModal();}}>
  
  button</button>   
     
  </th>
  <th>  
    <button 
  
      className="btn btn-success">Success</button>
     </th>
    </tr>
      ))}
           </tbody>
        </table>

          <Modal isOpened={display}
            closeModal ={closeModal}  > 
         
               <h1>modal header</h1>
               <p>{getAllData}</p>
               </Modal>
                  </div>

               </div>
       )
         }

        export default Customsite
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Here getAllData is a function that holds the value of the data so by directly calling that inside the <p> tag it will not print the data.

import React, { useState } from 'react';
 import Modal from './reusable/modal';
 import useFetchData from './hooks/useFetchData';

  const Customsite = ()=> {

const {
    data,
    loading,
  } = useFetchData();

 
const [display, openModal] = useState(false);
const [seletcedData, setSelectedData] = useState();
   
 const closeModal = () =>{
    openModal(false);
}


const openedModal = () =>{
  openModal(true);
}

   
const getAllData = (data) =>{

      console.log(data);
      setSelectedData(data);
      openedModal();
     }

return(


    <div>
      
 <div className='conatiner'>

     <table className="table">
  <thead>
   <tr>
  <th>id</th>
  <th>titel</th>
  <th>body</th>
  <th>actions</th>
  <th>details</th>
</tr>
   </thead>
<tbody>
      {data.map(posts =>(

  <tr key={posts.id}>
  <th>{posts.id}</th>
  <th>{posts.title}</th>
  <th>{posts.body}</th>
  <th><button className="btn btn-primary"
   onClick={()=> { getAllData(posts);}}>
  
  button</button>   
     
  </th>
  <th>  
    <button 
  
      className="btn btn-success">Success</button>
     </th>
    </tr>
      ))}
           </tbody>
        </table>
         {display &&
          <Modal isOpened={display}
            closeModal ={closeModal}  > 
         
               <h1>modal header</h1>
               <p>{selectedData?.title}</p>
               </Modal>
}
                  </div>

               </div>
       )
         }

        export default Customsite

Here is the solution and you can create multiple <p> tags to display the required data.

about 4 years ago · Juan Pablo Isaza Denunciar

0

make a state for data

   const [tableData, setTableData] = useState({})

after fetching data :

  const getAllData = (data) =>{
  setTableData(data)
  console.log(data);
 }

then on Modal:

       <Modal isOpened={display}
         closeModal ={closeModal}  > 
         <h1>{tableData.title}</h1>
         <p>{tableData.body}</p>
        </Modal>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try like this

import React, { useState } from "react";
import Modal from "./reusable/modal";
import useFetchData from "./hooks/useFetchData";

const Customsite = () => {
  const { data, loading } = useFetchData();

  const [display, openModal] = useState(false);
  const [rowData, setRowData] = useState();

  const closeModal = () => {
    openModal(false);
  };

  const openedModal = () => {
   openModal(true);
  };

return (
  <div>
    <div className="conatiner">
    <table className="table">
      <thead>
        <tr>
          <th>id</th>
          <th>titel</th>
          <th>body</th>
          <th>actions</th>
          <th>details</th>
        </tr>
      </thead>
      <tbody>
        {data.map((posts) => (
          <tr key={posts.id}>
            <th>{posts.id}</th>
            <th>{posts.title}</th>
            <th>{posts.body}</th>
            <th>
              <button
                className="btn btn-primary"
                onClick={() => {
                  setRowData(posts);
                  openedModal();
                }}
              >
                button
              </button>
            </th>
            <th>
              <button className="btn btn-success">Success</button>
            </th>
          </tr>
        ))}
      </tbody>
    </table>

    <Modal isOpened={display} closeModal={closeModal}>
      <div>
        <h1>modal header</h1>
        <table className="table">
          <thead>
            <tr>
              <th>id</th>
              <th>titel</th>
              <th>body</th>
            </tr>
          </thead>
          <tbody>
            {
               <tr key={rowData?.id}>
                <th>{rowData?.id}</th>
                <th>{rowData?.title}</th>
                <th>{rowData?.body}</th>
              </tr>
            }
          </tbody>
        </table>
      </div>
    </Modal>
  </div>
</div>

); };

export default Customsite;

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