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

85
Visualizações
Reactjs reusable components using same data and onclick function

I am trying to implement reusable component where data will be removed on the click function of the specific component.

But on click of one components removes the other component's data since I am using same data for reusable components. Is there a better way to do this?

my app.js file

import React from 'react';
import './App.css';
import ShotList from "./List"

function App() {
  const [data,setData] = React.useState([
    {
      name1:"asdsad",
      key: 0
    },
    {
      name1:"asdrsad",
      key: 1
    },
    {
      name1:"asdrsad",
      key: 2
    }
  ]);

  const removeData = (index) => {
    setData(data.filter((value) => value.key !== index))
  }
  
  return (
    <div className="App">
      <ShotList data={data} removeData={removeData}></ShotList>
      <ShotList data={data} removeData={removeData}></ShotList>
    </div>
  );
}

export default App;

my list.js file

import React from "react";

const ShotList = (data) => {
    console.log(data)
  return (
    <>
      {data.data.map((value,key) => (
        <p onClick={() => {console.log(key);data.removeData(key)}} key={key}>{value.name1}</p>
      ))}
      <p>123</p>
    </>
  );
};

export default ShotList;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

My suggestion will be as follows:

  • Once you pass data to the child components, mantain them as local state then clear the data on that components local state. i.e
import React from 'react';
import './App.css';
import ShotList from "./List"

function App() {
  const [data,setData] = React.useState([
    {
      name1:"asdsad",
      key: 0
    },
    {
      name1:"asdrsad",
      key: 1
    },
    {
      name1:"asdrsad",
      key: 2
    }
  ]);


  return (
    <div className="App">
      <ShotList data={data}></ShotList>
      <ShotList data={data}></ShotList>
    </div>
  );
}

export default App;

On the child/Shortlist Component:

import React from "react";

const ShotList = (data) => {
  const [localData, setLocalData] = useState(data)

 const removeData = (index) => {
    setLocalData(localData.filter((value) => value.key !== index))
  }


  return (
    <>
      {localData.map((value,key) => (
        <p onClick={() => {console.log(key);removeData(key)}} key={key}>{value.name1}</p>
      ))}
      <p>123</p>
    </>
  );
};

export default ShotList;
about 4 years ago · Juan Pablo Isaza Relatório

0

You cannot make this line twice:

<ShotList data={data} removeData={removeData}></ShotList>
<ShotList data={data} removeData={removeData}></ShotList>

because it will render your data twice and because of that elements inside .map would have the same key so removing one of them will remove also all that have the same key value.

If you want to have more elements just add new ones inside data state like:

const [data,setData] = React.useState([
    {
      name1:"asdsad",
      key: 0
    },
    {
      name1:"asdrsad",
      key: 1
    },
    {
      name1:"asdrsad",
      key: 2
    },
    {
      name1:"new one",
      key: 3
    },
    {
      name1:"fourth one",
      key: 4
    },
    {
      name1:"fifth one",
      key: 5
    }
 ]);
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