-
First some feedback regarding Fragments:Since this feature was released, it really complicated the usage of fragments in my opinion: Unleash the power of fragmens Maybe I was doing something wrong, but because of the fragmentRefs it felt like a lot of extra work to :
const DownloadsSection = (props: {
downloads: FragmentType<typeof BrandDownloadFragment>[]
sectionTitle: string
}) => {
const downloads = useFragment(BrandDownloadFragment, props.downloads) I really preferred how things were before this feature. I needed the type from a fragment? Just import that fragment and I'm done. I disabled fragment masking for now, as per this suggestion: How to disable fragment masking That seems to be working the way I was used to. Still curious, with fragment masking enabled, how to type a state variable?: Question: How to type a useState variable with a Fragment?I tried this: type Product = FragmentType<typeof ProductFragment>
export const AddProductForm = () => {
const [selectedProduct, setSelectedProduct] = useState<Product>(null) But sadly it will have the fragmentRef so I can't really setState. The following results in a type error: results.map((result: Product, i) => {
const product = useFragment(ProductFragment, result)
return (
<button onClick={() => setSelectedProduct(product)}>
...
</button>
)
})
} So wondering what I was doing wrong and how you would do this? I also tried using the ProductDetailsFragment directly: import { ProductDetailsFragment } from '@/types/gql/graphql' const [selectedProduct, setSelectedProduct] = useState<ProductDetailsFragment>(null) Which results in this error on the setSelectedProduct:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To pass the result of type Product = DocumentType<typeof ProductFragment>
export const AddProductForm = () => {
const [selectedProduct, setSelectedProduct] = useState<Product>(null) |
Beta Was this translation helpful? Give feedback.
To pass the result of
useFragment
to the state useDocumentType
, e.g.: