Hi! I’m trying to build a flexible Gatsby template for eCommerce sites. To make it work with Snipcart, I need a BuyButton that can vary its data-item-custom[x] fields based on a GraphQL pull. So far I can’t find a way to make that work.
The problem I’m running into is that the BuyButton accepts variables on the right side of the equals sign just fine, but only accepts hard-coded values on the left side of the equals sign. So this works:
<BuyButton
...
data-item-custom1-name={item.modifierArray[0].name}
...
>
But this doesn’t:
const exampleVar = "data-item-custom1-name";
<BuyButton
...
{exampleVar}={item.ModifierArray[0].name}
...
>
Is this possible right now in Snipcart?
Related: I tried calling BuyButton as a function call and then building out the logic in a separate function, but either Gatsby or Snipcart won’t let me do that; it breaks in a way that I find weird and opaque. BuyButton only works when built in the main return() call for the default function.
Edit: I realized I should include an example of this. This works:
export default function ItemPageTemplate({data}) {
...
return(
...
<BuyButton
...
>
);
}
This doesn’t work:
export default function ItemTemplatePage{
function BuyButton({ item }) {
return (
<BuyButton
...
>
);
}
// Here's the return statement for ItemPageTemplate,
// where we call the function <BuyButton>:
return(
...
<BuyButton item={item} />
...
);
}
Thanks for your time!