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

347
Views
How to Delete specific component from an useState array?

The function handleDeleteItem always deletes the last item from the array, I don't know why, and I want to delete the specific component that I clicked the button

export default function Home() {
  const [PostArray, setPostArray] = useState<React.ReactNode[]>([]);

  let component = <PostSheet />;

  function handleClick(event: React.MouseEvent<HTMLButtonElement>) {
    event.preventDefault();
    if (PostArray.length < 8) {
      setPostArray((OldArray: any) => [...OldArray, component]);
    }
  }

  const handleRemoveItem = useCallback(
    (todo: any) => {
      let newPostArray = [...PostArray];
      newPostArray.splice(PostArray.indexOf(todo), 1);
      setPostArray(newPostArray);
    },
    [PostArray]
  );
  const MappedArray = PostArray.map((e) => (
    <PostSheet click={handleRemoveItem} />
  ));

  return (
    <DefaultLayout click={handleClick}>
      <Wrapper>{MappedArray}</Wrapper>
    </DefaultLayout>
  );
}
about 4 years ago · Santiago Gelvez
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!