Can't use hooks conditionally - any workarounds? #1391
Unanswered
catherineluse
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Since you can't conditionally fetch resources with the
useQuery
hook, how would you create a component that sometimes needs to fetch something and sometimes doesn't?The problem I keep running into is with create/edit forms. I want exactly the same fields in both create and edit mode, but different data fetched from the back end.
Specifically, the existing data should only be fetched in edit mode, but not create mode:
And then on submit, I need to do separate mutations for create mode and edit mode, but otherwise everything is the same. But I can't conditionally use a
useMutation
hook either.So far, I have always created separate components for create forms and edit forms due to the inability to conditionally use these hooks. But my (small) code base is already starting to have too much duplicated code, and it's harder to maintain because I have to make each form change in two places. So what's the best practice for a situation like this?
Beta Was this translation helpful? Give feedback.
All reactions