Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
¿La forma correcta de realizar una llamada API dentro de div?

Así que actualmente estoy tratando de mostrar datos en una tabla. Estos datos provienen de 2 tablas separadas en la base de datos con claves externas. Obtengo mi lista usando esta llamada:

 useEffect(()=>{ axios.get("http://localhost:3001/stores").then((response)=>{ setlistofStores(response.data) //State which contains the response from the API request }); }, []);

Entonces puedo obtener la lista de tiendas y mostrarlas en la tabla sin problemas usando este código:

 <TableBody> {listofStores.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map((row) => ( <TableRow key={row.tenantName}> <TableCell> <Grid container> <Grid item lg={2}> <Avatar alt={row.unit} src='.' className={classes.avatar}/> </Grid> <Grid item lg={10}> <Typography className={classes.name}>{row.unit}</Typography> </Grid> </Grid> </TableCell> <TableCell>{row.contactName}</TableCell> <TableCell> <Typography className={classes.status} style={{ flex: 'center', backgroundColor: ((row.industry === 'Apparel' && 'purple') || (row.industry === 'F&B' && 'grey') || (row.industry === 'Admin' && 'red') || (row.industry === 'Tech' && 'blue')) }} >{row.industry}</Typography> </TableCell> <TableCell>{row.primaryEmail}</TableCell> <TableCell>{row.primaryPhone}</TableCell> <TableCell className={classes.stores}>1</TableCell> <TableCell ><button className={classes.viewButton} onClick={()=>{navigate(`/store/${row.id}`)}}>View</button></TableCell> </TableRow>

Ahora quiero ejecutar esta API dentro de cada fila para usar el Inquilino para mostrar sus datos:

 useEffect(() => { axios.get(`http://localhost:3001/store/byId/${id}`).then((response) => { setTenant(response.data); }); }, []);

¿Cuál es la forma correcta de hacer esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

useEffect con dependencias vacías es el bueno para su situación. Puede crear un nuevo componente para obtener detalles y, al hacer clic, lleve al usuario a ese componente (página). Y allí puede llamar a la API para obtener más detalles. (o puede ser una ventana emergente. Realmente depende del diseño de la interfaz de usuario)

 const TenantDetails = ({ tenantId, ...props }) => { const [tenantData, setTenantData] = useState(null); useEffect(() => { axios.get(`http://localhost:3001/store/byId/${tenantId}`).then((response) => { setTenantData(response.data); }); }, []); return ( // your UI implementation <> tenantData ? <div> ... </div> : <div>loading</div> </> ) }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!