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

313
Views
Cómo filtrar y eliminar un elemento usando el kit de herramientas redux

Cuando hago clic en el botón Eliminar en cada elemento, quiero eliminar el elemento con un filtro. Pero no sé cómo resolverlo correctamente. Estoy un poco confundido en este punto. Espero que alguien pueda explicar esto. Muchas gracias.

cartSlice.js

 const cartSlice = createSlice({ name: 'cart', initialState:{ products: [], quantity: 0, total: 0, }, reducers: { deleteItem: (state, action) => { //filter right here state.products = action.payload.idItem; state.quantity -= 1; state.total -= action.payload.price * action.payload.quantity; }, }, });

Carrito.jsx

 const Cart = (props) => { const dispatch = useDispatch(); const cart = useSelector((state) => state.cart); const items = useSelector((state) => state.cart.products); const handleDeleteItem = (idItem, price, quantity) => { const filter = items.filter((item) => item.itemId !== idItem); console.log('idItem', idItem); console.log('filter', filter); dispatch(deleteItem({ idItem, price, quantity })); }; return ( <> {cart.products.map((product) => ( <tr className={styles.body} key={product._id}> <td className={styles.column}> <span className={styles.quantity}>{product.quantity}</span> </td> <td className={styles.column}> <span className={styles.total}>${product.price * product.quantity}</span> </td> <td className={styles.column}> <button onClick={() => handleDeleteItem(product.idItem, product.price, product.quantity)} className={styles.deleteItem} > Delete </button> </td> </tr> ))} </> ); }; export default Cart;

Imagen del botón

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En su reductor, dice que el nuevo producto es la clave idItem:

 reducers: { deleteItem: (state, action) => { //filter right here state.products = action.payload.idItem; state.quantity -= 1; state.total -= action.payload.price * action.payload.quantity; }, },

Pero en su carrito, no pasa el filtro como parámetros:

 const handleDeleteItem = (idItem, price, quantity) => { const filter = items.filter((item) => item.itemId !== idItem); console.log('idItem', idItem); console.log('filter', filter); dispatch(deleteItem({ idItem, price, quantity })); };

¿No debería ser

 const handleDeleteItem = (idItem, price, quantity) => { const filter = items.filter((item) => item.itemId !== idItem); dispatch(deleteItem({ idItem: filter, price, quantity })); };
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!