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

182
Views
¿Cómo llamo a la función de reacción en el mapa js?

Tengo el siguiente código, el problema está en la parte donde llamo a la función RemoveProduct en la función de mapa:

 function AddOrder() { const d = new Date(); let text = d.toString(); const [addOrder] = useMutation(queries.ADD_ORDER); const [editUser] = useMutation(queries.EDIT_USER_CART); const [editProduct] = useMutation(queries.EDIT_PRODUCT); function RemoveProduct (x) { let getProd = useQuery(queries.GET_PRODUCTS_BY_ID, { fetchPolicy: "network-only", variables: { _id : x._id }, }); console.log(getProd.quantity) console.log(x.quantity) let a = getProd.quantity - x.quantity console.log(a) editProduct({ variables: { _id : x._id, quantity: a }, }); return null; } const { currentUser } = useContext(AuthContext); if (error) { return <h1> error</h1>; } else if (loading) { return <h1> loading</h1>; } else if (data && getUserOrders.data && currentUser && data.getUser.cart.length > 0) { let newCart = []; let total = 0; for (let i = 0; i < data.getUser.cart.length; i++) { total += data.getUser.cart[i].price * data.getUser.cart[i].quantity; newCart.push({ orderedQuantity: data.getUser.cart[i].quantity, _id: data.getUser.cart[i]._id, name: data.getUser.cart[i].name, image: data.getUser.cart[i].image, price: data.getUser.cart[i].price, }); } newCart.map((x) => RemoveProduct(x)) editUser({ variables: { id: currentUser.uid, cart: [], }, }); } } export default AddOrder;

Recibo el siguiente error cuando ejecuto este código:

 Uncaught Error: Rendered more hooks than during the previous render.

¿Cómo puedo arreglar esto? Intenté crear un componente separado para RemoveProducts y llamar a esta función, pero tampoco funcionó.

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

0

  1. Solo ganchos de llamada en el nivel superior
  2. No llame a ganchos dentro de bucles, condiciones o funciones anidadas
  3. Siempre use ganchos en el nivel superior de su función React, antes de cualquier retorno anticipado
  4. Solo llame a los ganchos desde los componentes de la función React o desde los ganchos personalizados.

Leí esto de los documentos aquí

Por lo tanto, intente no anidar la función RemoveProduct o eliminar el gancho llamado dentro de ella.

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!