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

96
Visualizações
update the specific item in map loop

My idea is when the mouse enters the item, extract item, it will update the element and change its text here is the code :

   {skillItems.map((item, index) => {
      const { id, name} = item;
      return (
        <li
          key={id}
          className="singleChart"
          onMouseOver={() => setOver(true)}
          onMouseLeave={() => setOver(false)}
        >{overEffect ? name : ""}</li>)

But when the mouse over the item, all the other items also change. what should I do?

Thank you

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

0

you need to save the id of the item too for the item of mouse over

skillItems.map((item, index) => {
  const { id, name} = item;
  return (
    <li
      key={id}
      className="singleChart"
      onMouseOver={() => setOver({value:true,id})}
      onMouseLeave={() => setOver({value:false,id})}
    >
      {(overEffect.value&&overEffect.id) ? name : ""}</li>)
about 4 years ago · Juan Pablo Isaza Relatório

0

In this case you need a bit more complex state object. I used an object to keep state for each item in the list. One way of doing is like this: https://codesandbox.io/s/https-stackoverflow-com-questions-69612695-update-the-specific-item-in-map-loop-69613214-69613214-z5o17?file=/src/App.js - let me know if this is accessible to you.

What is done here is that we had to construct state object using reduce method and later we have to take care how we set new state (not to override something).

import React from "react";

function App() {
  const skillItems = [
    {
      id: 1,
      name: "111"
    },
    {
      id: 2,
      name: "222"
    },
    {
      id: 3,
      name: "333"
    }
  ];
  const stateObj = skillItems.reduce((acc, curr) => {
    const keys = Object.keys(curr);
    if (!acc) {
      return Object.assign({}, { [keys[0] + curr.id]: true });
    }
    return Object.assign(acc, { [keys[0] + curr.id]: true });
  }, null);

  const [overEffect, setOver] = React.useState(stateObj);
  console.log("overEffect", overEffect);

  return (
    <div className="App" style={{ backgroundColor: "pink" }}>
      {skillItems.map((item, index) => {
        const { id, name } = item;
        const key = "id" + id;
        return (
          <li
            key={id}
            onMouseOver={() => setOver({ ...overEffect, [key]: false })}
            onMouseLeave={() => setOver({ ...overEffect, [key]: true })}
          >
            {overEffect[key] ? name : ""}
          </li>
        );
      })}
    </div>
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <App />
  </StrictMode>,
  rootElement
);
about 4 years ago · Juan Pablo Isaza Relatório

0

If you need to change only one item then you can have only one state which will show the index of item in which moues will be overed. So you can do this

{skillItems.map((item, index) => {
      const { id, name} = item;
      return (
        <li
          key={id}
          className="singleChart"
          onMouseOver={() => setOver(index)}
          onMouseLeave={() => setOver(-1)}
        >{overEffect === index ? name : ""}</li>)

or the same thing whith ids

{skillItems.map((item, index) => {
      const { id, name} = item;
      return (
        <li
          key={id}
          className="singleChart"
          onMouseOver={() => setOver(id)}
          onMouseLeave={() => setOver(null)}
        >{overEffect === id ? name : ""}</li>)
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