useFragment: why use the hook naming convention? #8503
-
I am trying to apply graphql-code-generator to an existing NextJS project. I do stuff like that inside the project: Notice how I use a map, and inside the map I use an functional id as the element key. If I want to keep the same using code generator and fragments, it turns out like this:
If I want to keep my functional id, I need to call "useFragment" inside my map function... It works, however it smells very bad: indeed, useFragment looks like a hook, and hooks cannot be used inside loops in React! Static analysers like ESLint will see an error. That made me wondering, why do we need all that "$fragmentRefs" boilerplate inside the generated code?
At least, I think useFragment should be renamed, it is not a hook. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @fromi, I've built the following working example that shows how to make a property usable for React loops: As you can see here, we request the field used as an iteration key inside and outside of the fragment: it can then be used properly: https://github.com/charlypoly/codegen-repros/blob/master/client-preset-fragments-loops/src/App.tsx#L30 Finally, on your last question regarding |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer! |
Beta Was this translation helpful? Give feedback.
Hi @fromi,
I've built the following working example that shows how to make a property usable for React loops:
As you can see here, we request the field used as an iteration key inside and outside of the fragment:
https://github.com/charlypoly/codegen-repros/blob/cffbfd5d8a0d835f8328250858ade371dcfbcc6c/client-preset-fragments-loops/src/App.tsx#L10-L18
it can then be used properly: https://github.com/charlypoly/codegen-repros/blob/master/client-preset-fragments-loops/src/App.tsx#L30
Finally, on your last question regarding
$fragmentRefs
; those are necessary to provide Fragment Masking capabilities (isolating data dependencies).You will find more information on this architecture principle…