Coder Social home page Coder Social logo

Comments (5)

jbaubree avatar jbaubree commented on September 21, 2024

Oh, i didn't see then function was provided, this is a solution. Feel free to close this issue if you think onSuccess and onError is too much for this library but i think it's important to have a specific hooks for error and success

from villus.

jbaubree avatar jbaubree commented on September 21, 2024

Update: onFulfilled function in then is only triggered on first call so it does not work with computed variables etc...

from villus.

jbaubree avatar jbaubree commented on September 21, 2024

@logaretm Do you think we can do the same thing for mutations ?

from villus.

logaretm avatar logaretm commented on September 21, 2024

@jbaubree Usually mutations are executed explicitly, so I don't think it is as common as queries but I don't see the harm. Feel free to PR it, and I guess you ran into needing it recently, would love to hear about it as well, use-case wise.

from villus.

jbaubree avatar jbaubree commented on September 21, 2024

@logaretm There are several reasons why I think it is necessary.
Let's take a simple example:
We are in a login page.

const { execute: signIn } = useMutation<{ signIn: { token: string } }>(SIGN_IN)

async function onSubmit() {
  signIn({ publicAddress, signature }).then(({data, error}) => {
    if (data) <-- Here we don't know if mutation is a success and i find data/error conditions are pains
      //...
    if (error)
      //...
  }
}

Second reason, i find code more readable like this:

const { execute: signIn } = useMutation<{ signIn: { token: string } }>(SIGN_IN, {
  onData(data) {
    setToken(data.signIn.token) <--- We are sure that data exists here
    router.push({ name: 'Root' })
  },
  onError(err) {
    ...
  },
})

async function onSubmit() {
  signIn({ publicAddress, signature })
}

If you confirm that you are ok with that, i will PR it when i will get some free time

from villus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.