Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
missing previous state with using function inside provider - nextjs

I have a provider like this:

import {createContext, useContext, useState} from 'react'

// Create Context object.
const CartContext = createContext({
   deliveryPersion:'own',//or other
   coupon:{},
   updateCoupon: (obj) => {},
   updateDeliveryPersion: (type) => {},
})



// Export Provider.
export function CartProvider(props) {

   const updateCoupon = (_coupon) => {
      setState({...state,coupon: _coupon})
   }


  const updateDeliveryPersion = (type) => {
     setState({...state,deliveryPersion: type})
   }



   const initState = {
      deliveryPersion:'own',
      coupon:{},
      updateCoupon:updateCoupon,
      updateDeliveryPersion:updateDeliveryPersion
   }

   const [state, setState] = useState(initState)
    
    return (
       <CartContext.Provider value={state}>
        {props.children}
       </CartContext.Provider>
    )
}

// Export useContext Hook.
export function useCartContext() {
    return useContext(CartContext);
}

My senario: I have two components. component1 and component1.

inside component1 I updated the deliveryPersion variable like this:

...
cartContext.updateDeliveryPersion('other')

inside component2 I updated coupon object:

const data = {/**/}
cartContext.updateCoupon(data)

at this here deliveryPersion changed to default value.(own value). in other word, I missing current value with using update function.

where is my problem?

for example first component:

const cartContext =  useCartContext()
cartContext.updateDeliveryPersion(type)
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda