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

218
Views
TypeError: getItem no es una función cuando se prueba un componente con ganchos de módulo de nodo

Estoy aprendiendo React y me quedé atascado probando uno de mis componentes. Uso react-use-cart para administrar el carrito, así es como se ve el código

 import React from "react"; import { Link } from "react-router-dom"; import { useCart } from "react-use-cart"; import PropTypes from "prop-types"; import handleQuantityChange from "../../logic/product/handleQuantityChange"; function AddToCart(props) { const {product, styles, loading} = props; const { addItem, inCart, getItem, updateItemQuantity } = useCart(); const productInCart = getItem(product.id);

La prueba se interrumpe en "productInCart = getItem (product.id)" y dice "TypeError: getItem no es una función". He estado atascado en esto durante varios días y realmente agradecería un poco de ayuda, es la primera vez que hago una pregunta, así que si necesita más información, hágamelo saber, gracias.

También sobre la prueba, pausas de prueba en la etapa de renderizado.

 import React from "react"; import { render, screen } from "@testing-library/react"; import AddToCart from "../AddToCart"; import { MemoryRouter as Router } from "react-router-dom"; describe("get item is not a function", () => { const product = { name: "test name", imgUrl: "test image", price: 555, shipping: 1000, id: 55 }; const styles = [] beforeEach(() => { render( <Router> <AddToCart product={product} styles={styles}/> </Router> ); });

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

0

Te falta CartProvider :

 {...} beforeEach(() => { render( <CartProvider> <Router> <AddToCart product={product} styles={styles}/> </Router> </CartProvider> ); });
about 4 years ago · Juan Pablo Isaza Report

0

¿Intentó registrar el valor de getItem para ver si realmente es una función? Aunque no estoy familiarizado con use-cart, parece que lo estás haciendo correctamente. Aparte de eso, a la función addToCart le falta una llave de cierre, supongo que simplemente no pegó el código completo.

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!