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

320
Visualizações
How to enable a button to delete items in an array?

I have a task that needs me to create a button that deletes certain items in an array. I have passed an array as a prop to the child component (). Now in the child component, whenever I click the delete button, it doesn't seem to work and I am struggling to find the problem.

Here's the link to the code: (App.js)

function App() {
  const [input, setInput] = useState("");
  const [list, setList] = useState([]);
  
  const handleSubmit = (e) => {
      e.preventDefault();
      setList([...list, {val: input, key: Math.floor(Math.random() * 11)}]);
  }

  const handleChange = (e) => {
       setInput(e.target.value)
  }
  return(
    <div className='App'>
     <header className='App-header'>
       <div className='Wrapper'>
         <div className='input-container'>
         <input type="text" onChange={handleChange} value={input} />
         <input type="submit" onClick={handleSubmit} />
         </div>
       <List list={list}/>
       </div>
     </header>
    </div>
  )
}


export default App;

And the link to the "" component that:

function List(props) {
    //handle delete. we return elements on a condition that the keys do not match
    const handleDelete = (key)=>{
        props.list.filter((items)=>{
            return items.key != key
        })
    }


  return (
      <>
          {
              props.list.map(function(listItem){
                  return (
                      <div className='lists' key={listItem.key}>
                          <p>{listItem.val}  <button onClick={handleDelete(listItem.key)}>Delete</button> </p>
                      </div>
                  )
              })
          }
      </>
  )
}

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

0

You need to pass setList to List component

<List list={list} setList={setList}/>

List

const handleDelete = (key)=>{
    const newList = props.list.filter((items)=>{
        return items.key != key
    })
    props.setList(newList)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The filter function is returning a new array, it is not modifying the existing array you have. Therefore, you have to assign the newly filtered list using the setList function. You can either pass the setList function as a prop to your child component to call it after filtering, or provide your child component a handleDelete callback function which you would define in the parent component (which will do the filtering and setting in the parent) via a prop, and then you just call it in the child component on click.

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