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

84
Views
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 answers
Answer question

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 Report

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 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!