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

286
Vistas
How do I get acces to only one item from map method in react js

My question may not be clear but here is my problem. Here is my card that I got from an array using the map method and display each item on each card. And I have triggered the "Edit" button so that it will show the hidden text(Want to see this in only one card). But when I click on only one card, all cards show that hidden message. Can you please help me?

I want to see that "Want to see this in only one card" text in that card where edit button is clicked

Here are my codes:

const [edit, setedit]= useState(false)  

<Grid container spacing={5} className="main-grid" >
{allitems.map((oneitem, index) => {
return (
<Grid item key={index} md={3} className="itemGrid" >
<Card className="card">
<CardContent>
<Typography className="" color="textSecondary" gutterBottom>
{oneitem.title}
</Typography>/
<p variant="h5" component="h2" className="description">
{oneitem.description}
</p>
<p className="" color="textSecondary">
Created At: {oneitem.createdAt}
</p>
<Button size="small" onClick={()=> deleted(oneitem._id)} >Delete</Button>
<Button size="small" onClick={()=>setedit(!edit)} >Edit</Button>  <-here is the problem
{edit && <h1>Want to see this in only one card</h1>}   
</CardContent>

here is the image

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Issue

You are using a single boolean edit state value to trigger the edit mode of all mapped elements.

Solution

Use some edit state that you can correlate to your data, like the index or an item id property. Since I don't see any GUIDs used I'll demo with index.

  1. Use the element index to identify what is in "edit" mode, null means nothing has the "edit" mode.

     const [editIndex, setEditIndex]= useState(null);
    
  2. Update the toggle button to toggle a new index or back to null if the same button is clicked

     <Button
       size="small"
       onClick={() => setEditIndex(editIndex => editIndex === index ? null : index)}
     >
       Edit
     </Button>
    
  3. Match the saved editIndex state to the currently mapped element to conditionally render the messaging.

     {editIndex === index && <h1>Want to see this in only one card</h1>}
    

Additional Note

I see you have a delete button:

<Button size="small" onClick={()=> deleted(oneitem._id)} >Delete</Button>

If you are deleting elements from the underlying data then you will want to not use the array index as the React key. Instead you should use a uniquely identifying property, like _id, of each element (they need only be unique among siblings). So instead of using the index to set the "edit" mode you would use _id instead.

over 4 years ago · Santiago Trujillo Denunciar

0

This problem comes up for you because you're using a common edit value throughout the map. So, if you change the main value of "edit" to "true" (as in this case), it will consider the edit as true for all the cards.

No, Here's what you can try doing ( I'm considering you can edit only one card at a time ) instead of keeping the edit as a boolean value (true|false) use it as an integer value to store the index of the card in your array.

Steps :

  1. By default keep edit = null
  2. the setEdit function can become something like this: (considering class based state)

setEdit(editVal) => {
  if(this.state.edit === editVal){
    this.setState({
      edit: null
      })
  }
  else{
    this.setState({
      edit: editVal
    }
  }
}

  1. and while calling the setEdit method call it like this:

onClick={()=> setEdit(index)}

  1. Now everytime you click the edit button the edit value either changes to a new index value or to a null.

  2. One last change is to replace this:

{edit && <h1>Want to see this in only one card</h1>}

with this

{edit===index && <h1>Want to see this in only one card</h1>}

Concluding:

We first moved the edit value to Integer or null, Then on clicking edit the index value of the element in the array is assigned to "edit". once this happens, we're checking if the edit value is equals to the index value (step 5), if so, it shows the message only for that card. Once you click edit again, the value of "edit" becomes null (if clicked for the same card).

over 4 years ago · Santiago Trujillo 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