Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
unable to remove react element onclick

I am trying to remove an element when i click on the x button. Below is my code

 const onRemoveItem = (el) => {
    var element = el;
    if (element != null) {
      element.remove();
    }
    //   chartInstance.resize();
  };

  const removeStyle = {
    position: "absolute",
    left: "11px",
    top: 0,
    cursor: "pointer"
  };

  return (
    <ResponsiveGridLayout layouts={layout}>
      <div key="1">
        <NewvsReturnVisitors />
        <span className="remove" style={removeStyle} onClick={onRemoveItem}>
          x
        </span>
      </div>
      <div key="2">
        <NewvsReturnVisitors />
        <span className="remove" style={removeStyle} onClick={onRemoveItem}>
          x
        </span>
      </div>
     <div key="3">
        <NewvsReturnVisitors />
        <span className="remove" style={removeStyle} onClick={onRemoveItem}>
          x
        </span>
      </div>
    </ResponsiveGridLayout>
  );

but it keep returning me remove is not a function, is there a way in react to remove an element like how it will work in html?

This is my codesandbox link

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can maintain an array of displayed items. If the id of a div is in the array just add a class to hide it.

const { useState } = React;

function Example() {

  const [ hidden, setHidden ] = useState([]);

  // When you click on the X get the
  // id of the closest div, and add it to state
  function handleClick(e) {
    const div = e.target.closest('div');
    const { id } = div.dataset;
    const newState = [...hidden, Number(id)];
    setHidden(newState);
  }

  // Small function that each div calls
  // to determine if the hidden class
  // should be applied
  function isHidden(id) {
    return hidden.includes(id) && 'hidden';
  }
  
  return (
    <div onClick={handleClick}>
      <div className={isHidden(1)} data-id="1">
        One&nbsp;
        <span class="remove">x</span>
      </div>
      <div className={isHidden(2)} data-id="2">
        Two&nbsp;
        <span class="remove">x</span>
      </div>
      <div className={isHidden(3)} data-id="3">
        Three&nbsp;
        <span class="remove">x</span>
      </div>
    </div>
  );
};

// Render it
ReactDOM.render(
  <Example />,
  document.getElementById("react")
);
span { cursor: pointer; color: red; }
.hidden { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda