You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We’re in a tricky spot with an imperative query. I came across this discussion by @TkDodo and this #1546, but our case is a little different.
We have a multi-step wizard form that’s not linear. It behaves more like a tree. The next form step depends on data from our backend.
Each step has a Next button, and based on some heavy logic (that cannot live in the frontend), the backend decides where the user should go.
Here’s a simplified pseudo-code example:
constAFormStepComponent=()=>{constonSubmit=()=>{// 1. Call the backend to figure out the next step// 2. Navigate to the step returned by the backend}return(<formonSubmit={handleSubmit}>
...
<buttontype="submit">Next</button></form>)}
Since the backend isn’t mutating anything and only returns the next step, it’s exposed as a GET endpoint.
Naturally, our first instinct is to use useQuery for GET requests, but in this case, we’re unsure what’s best.
Cleaner code. No workarounds for calling api.getNext.
Cons
Goes against TanStack Query + our convention of “GET = useQuery”.
The term mutation feels wrong here since nothing is being mutated.
Option 3: useMutation just as option 2 but change the names not to confuse developers
exportconstuseNextGetImperative=()=>{returnuseMutation({mutationFn: api.getNext,})}constAFormStepComponent=()=>{const{ formInfo }=useForm()// pseudo-codeconstnextGetImperative=useNextGetImperative()constonSubmit=(formInfo)=>{nextGetImperative.mutate(formInfo)// still has the word mutate, which can confuse devs}return(<formonSubmit={handleSubmit}>
...
<buttontype="submit">Next</button></form>)}
Pros
same as option 2 pros
Cons
same as option 2 cons
still has the word mutate when doing nextGetImperative.mutate, which can confuse devs
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Imperative Query in a Non-Linear Wizard Form
Hello all,
// First... our package.json
We’re in a tricky spot with an imperative query. I came across this discussion by @TkDodo and this #1546, but our case is a little different.
We have a multi-step wizard form that’s not linear. It behaves more like a tree. The next form step depends on data from our backend.
Each step has a Next button, and based on some heavy logic (that cannot live in the frontend), the backend decides where the user should go.
Here’s a simplified pseudo-code example:
Since the backend isn’t mutating anything and only returns the next step, it’s exposed as a
GET
endpoint.Naturally, our first instinct is to use
useQuery
for GET requests, but in this case, we’re unsure what’s best.Here are the options we’re considering:
Option 1:
useQuery
Pros
Cons
onSubmit
.Option 2:
useMutation
Pros
api.getNext
.Cons
Option 3:
useMutation
just as option 2 but change the names not to confuse developersPros
Cons
nextGetImperative.mutate
, which can confuse devsOption 4: Don’t Use TanStack Query
Pros
Cons
The Question
Given that:
onSubmit
flow.👉 Which approach makes the most sense here?
👉 any other approach we are not thinking about or don't know about?
Thanks!!!
PS: We are aware of TanStack DB but, right now it's not possible for us to give it a try
Beta Was this translation helpful? Give feedback.
All reactions