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

236
Views
Cómo eliminar un elemento al hacer clic en Reaccionar

Estoy tratando de eliminar un elemento en React haciendo clic en un botón. Actualmente, mi código devuelve Cannot read properties of undefined (reading 'ProductID') y no estoy seguro de por qué, ya que estoy accediendo a él a través de una identificación y debería funcionar. ¿Cómo puedo hacer que mi código funcione correctamente y eliminar el elemento al hacer clic? Aquí está mi intento:

 import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { Grid, GridColumn as Column } from '@progress/kendo-react-grid'; import { sampleProducts } from './sample-products'; const allInEdit = sampleProducts.map((item) => Object.assign( { inEdit: true, }, item ) ); const deleteItem = (item) => { let index = sampleProducts.findIndex( (record) => record.ProductID === item.ProductID ); data.splice(index, 1); return sampleProducts; }; const MyCommandCell = () => { return ( <button className="k-button k-grid-remove-command" onClick={() => deleteItem()} > Remove </button> ); }; const App = () => { const [data, setData] = React.useState(allInEdit); const itemChange = (e) => { let newData = data.map((item) => { if (item.ProductID === e.dataItem.ProductID) { item[e.field || ''] = e.value; } return item; }); setData(newData); }; return ( <Grid data={data} editField="inEdit" onItemChange={itemChange}> <Column field="ProductID" title="Id" width="50px" editable={false} /> <Column field="ProductName" /> <Column field="FirstOrderedOn" editor="date" format="{0:d}" /> <Column field="UnitsInStock" width="150px" /> <Column field="Discontinued" width="50px" editor="boolean" /> <Column cell={MyCommandCell} width="240px" /> </Grid> ); }; ReactDOM.render(<App />, document.querySelector('my-app'));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe mover sus funciones deleteItem y MyCommandCell dentro de su componente de aplicación para usar setData. También necesita leer los datos de la fila actual del elemento de datos del dataItem de la fila actual. Ese valor debe pasarse a la función deleteItem como accesorio

Puedes usar este código:

 import * as React from "react"; import * as ReactDOM from "react-dom"; import { Grid, GridColumn as Column } from "@progress/kendo-react-grid"; import { sampleProducts } from "./sample-products"; const allInEdit = sampleProducts.map((item) => Object.assign( { inEdit: true }, item ) ); const App = () => { const [data, setData] = React.useState(allInEdit); const deleteItem = (item) => { let newProducts = data.filter( (record) => record.ProductID !== item.ProductID ); setData(newProducts); }; const MyCommandCell = (item) => { return ( <button className="k-button k-grid-remove-command" onClick={() => deleteItem(item.dataItem)} > Remove </button> ); }; const itemChange = (e) => { let newData = data.map((item) => { if (item.ProductID === e.dataItem.ProductID) { item[e.field || ""] = e.value; } return item; }); setData(newData); }; return ( <Grid data={data} editField="inEdit" onItemChange={itemChange}> <Column field="ProductID" title="Id" width="50px" editable={false} /> <Column field="ProductName" /> <Column field="FirstOrderedOn" editor="date" format="{0:d}" /> <Column field="UnitsInStock" width="150px" /> <Column field="Discontinued" width="50px" editor="boolean" /> <Column cell={MyCommandCell} width="240px" /> </Grid> ); }; ReactDOM.render(<App />, document.querySelector("my-app"));

Puede echar un vistazo a este sandbox para ver un ejemplo de trabajo en vivo.

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!