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

166
Views
It is necessary that when the component is removed, it does not just disappear, but leaves behind component with button

thank you in advance, however, before answering the question, read carefully what I ask for help with all due respect. What i need:

I need that when the delete button is clicked, the component is not only deleted, but also leaves behind another button, by clicking on which, the remote component is rendered back

Functionality that already works: rendering a component on click, as well as deleting by a button

import React, {useState} from 'react';

import ReactDOM from 'react-dom';

interface IParams {
    id: number;
}

interface IBlock {
    deleteBlock(blockToDelete: number) : void
    id: number
}

function App() {

const [newBlock, setNewBlock] = useState([] as IParams[])

const createOnClick = () => {
    const newId =  {
        id: newBlock.length + 1
    }
    setNewBlock([...newBlock, newId])
}

const deleteBlock = (blockToDelete: number) => {
    setNewBlock(
        newBlock.filter((x) => {
            return x.id !== blockToDelete
        })
    )
}

const FunctionalBlock: React.FC<IBlock>  = ({id, deleteBlock}) => {
    return (
        <div style={{display: 'flex', maxWidth: '300px', justifyContent: 'space-between'}}>
            <div>i was created {id} times</div>
            <button onClick={() => {
                deleteBlock(id)
            }}>now delete me</button>
        </div>
    )
}

return (
    <div className="App">
        <button onClick={createOnClick}>
            New block
        </button>
        {
            newBlock.map((x) => (
                <FunctionalBlock id={x.id} key={x.id} deleteBlock={deleteBlock}/>
            ))
        }
    </div>
);
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Why not just set a state on the block?

const {useState} = React
const FunctionalBlock = ({ id, idx, isDeleted, toggleBlockState }) => {
  return (
    <div
      style={{
        display: "flex",
        maxWidth: "300px",
        justifyContent: "space-between"
      }}
    >
    {
      !isDeleted
      ? <React.Fragment>
        <div>i was created {idx} times</div>
          <button
            onClick={toggleBlockState}
          >
            now delete me
          </button>
        </React.Fragment>
      : <button onClick={toggleBlockState}>REVIVE BLOCK</button>
    }
    </div>
  )
  ;
};

const getNewBlock = (idx) => ({
  id: Date.now(),
  idx,
  isDeleted: false,
})

const toggleIsDeletedById = (id, block) => {
  if (id !== block.id) return block
  return {
    ...block,
    isDeleted: !block.isDeleted
  }
}

const App = () => {
  const [newBlock, setNewBlock] = useState([])
  
  const createOnClick = () => {
    const block = getNewBlock(newBlock.length + 1)
    setNewBlock([...newBlock, block])
  }
  
  const toggleBlockStateById = (id) => {
    setNewBlock(newBlock.map((block) => toggleIsDeletedById(id, block)))
  }
  
  return (
    <div>
      <div>NEW BLOCK</div>
      <div><button onClick={createOnClick}>ADD NEW BLOCK +</button></div>
      <div>
        {
          newBlock.map(block => <FunctionalBlock {...block} toggleBlockState={() => toggleBlockStateById(block.id)}/>)
        }
      </div>
    </div>
  );
};

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>

<div id="root"></div>

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