I need help with Dispatch within the context in React

Asked

Viewed 18 times

0

I made a resquettes to save an image in our API but I’m having problems in the Dispatch of this resquet and worked:

const trocarAssinatura = async img => {
    dispatch({ type: actions.SET_LOADING_ASSINATURA, payload: true })
    const data = new FormData()
    data.append('assinatura', img)
    data.append('fornecedor', maquinadeestadosstore.getIdEmpresa())
    try {
      const res = await Api.post('/portal/proposta_assinatura/', data)
      dispatch({ type: actions.SET_ASSINATURA, payload: res.data.assinatura })
      
    } catch (err) {
      AlertaError('Erro ao enviar assinatura.')
      console.log(err)
    } finally {
      ToastSuccess('Assinatura enviada.')
      dispatch({ type: actions.SET_LOADING_ASSINATURA, payload: false })
    }
  }

In the case Dispatch({ type: actions.SET_ASSINATURA, payload: res.data.signature }) is not working. I migrated this function from Mobx to Context and mobx was like this:

@action handleAssinatura = img => {
    this.loadingAssinatura = true
    const data = new FormData()
    data.append('assinatura', img)
    data.append('fornecedor', maquinadeestadosstore.getIdEmpresa())
    Api.post('/portal/proposta_assinatura/', data)
      .then(res => {
        this.assinatura = res.data.assinatura
        ToastSuccess('Assinatura enviada.')
      })
      .catch(err => {
        AlertaVermelho('Erro ao enviar assinatura.')
        console.log(err)
      })
      .finally(() => {
        this.loadingAssinatura = false
      })
  }

Can someone help me?

  • Hello, João. You migrated to React Context, do you also refer to useReducer? How is your Reducer? And what error did you have before?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.