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

142
Views
¿Cómo evitar que las tarjetas de interfaz de usuario de material expandibles se abran al mismo tiempo?

Actualmente tengo dos tarjetas expandibles Material UI y todo funciona como se esperaba menos el hecho de que ambas tarjetas se expanden al mismo tiempo.

Mi archivo de configuración se parece a:

 module.exports = { featuredSection: [ { id: 1, name: 'beer-image-1', title: 'Mango Cart', description: 'A light Wheat Ale bursting with lots of fresh mango flavor and aroma, finishing crisp and refreshing.', malt: '2-row, Wheat', hops: 'Bravo', notes: 'Light, refreshing Wheat Ale with lots of fresh Mango flavor and aroma.', }, { id: 2, name: 'hero-image', title: 'test', description: 'A light Wheat Ale bursting with lots of fresh mango flavor and aroma, finishing crisp and refreshing.', malt: 'test', hops: 'test', notes: 'test', }, ], };

Mi archivo de componente Featured.js se ve así:

 const Featured = ({ featuredSection }) => { const classes = useStyles(); const [expanded, setExpanded] = useState(false); const handleExpandClick = (id) => { setExpanded(!expanded); }; const card = featuredSection.map((item, i) => { return ( <div key={item.id}> <CardWrapper> <Card className={classes.root}> <CardMedia className={classes.media} image={require(`../images/${item.name}.jpg`).default} title={item.title} /> <CardContent> ... </CardContent> <CardActions disableSpacing> <IconButton style={{ color: '#f5f5f5' }} className={clsx(classes.expand, { [classes.expandOpen]: expanded, })} onClick={(id) => handleExpandClick(item.id)} aria-expanded={expanded} aria-label='show more'> <ExpandMoreIcon /> </IconButton> </CardActions> <Collapse in={expanded} timeout='auto' unmountOnExit> ... </Collapse> </Card> </CardWrapper> </div> ); }); return <>{card}</>; }; export default Featured;

Supongo que hay algo que debo cambiar con el estado o hacer coincidir ciertos valores, como la identificación del objeto del archivo de configuración, pero no estoy exactamente seguro de lo que se debe hacer para poder abrir cada tarjeta individualmente. Recreé el problema aquí .

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

0

Tema

Tu corazonada va en la dirección correcta. El problema que tiene es usar el único estado expanded para alternar la expansión de la tarjeta. Cuando cualquier carta cambia al estado único, activa todas las cartas.

Solución

Use un objeto para almacenar el valor alternado por id, y use la id del elemento mapeado actual para verificar el valor.

 const [expanded, setExpanded] = useState({}); const handleExpandClick = (id) => { setExpanded(expanded => ({ ...expanded, [id]: !expanded[id], })); }; ... <Collapse in={expanded[item.id]} timeout="auto" unmountOnExit > ... </Collapse>

Edite cómo evitar que las tarjetas de interfaz de usuario de material expandible se abran al mismo tiempo

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!