If I have a simple UI component that displays some some product info. And it contains it's own fragment like so:
const Product = ({detail}) => {
const data = useFragment(
graphql`
fragment Product_detail on Product {
title
detail
}
`,
detail
)
return (
<div>
<h1>{data.title}</h1>
<p>{data.detail}</p>
</div>
}
And it is use it inside a parent component that looks like this:
const Page1 = ({content}) => {
const data = useFragment(
graphql`
fragment Page1_content on Page1Info {
oneProduct {
...Product_detail
}
}
`,
detail
)
return <Product detail={data.oneProduct}/>
}
How could I reuse the Product
component on a different page, where the parent's graphql fragment returns an array of Products, instead of just one product?
const Page2 = ({content}) => {
const data = useFragment(
graphql`
fragment Page2_content on Page2Info {
allProducts { //returns an array of products
title
...Product_detail
}
}
`,
detail
)
return (
<div>
{data.allProducts.map(product => <button>{product.name}</button>)}
{*/Display the product detail of the product who's button is clicked*/}
<Product detail={data.allProducts}/>
</div>
}
I am using Relay as my graphql client. But I'm assuming the principal are the same across all clients?