use Shopify Cookieshooks
hooks
Sets Shopify user and session cookies and refreshes the expiry time.
Manages Shopify cookies. If option is false, Shopify cookies will be removed.
Anchor to useShopifyCookies-parametersParameters
- UseShopifyCookiesOptions
Anchor to useShopifyCookies-returnsReturnsvoid
void
UseShopifyCookiesGeneratedType
- options
UseShopifyCookiesOptions
void
export function useShopifyCookies(options?: UseShopifyCookiesOptions): void {
const {hasUserConsent = false, domain = ''} = options || {};
useEffect(() => {
const cookies = getShopifyCookies(document.cookie);
/**
* Set user and session cookies and refresh the expiry time
*/
if (hasUserConsent) {
setCookie(
SHOPIFY_Y,
cookies[SHOPIFY_Y] || buildUUID(),
longTermLength,
domain,
);
setCookie(
SHOPIFY_S,
cookies[SHOPIFY_S] || buildUUID(),
shortTermLength,
domain,
);
} else {
setCookie(SHOPIFY_Y, '', 0, domain);
setCookie(SHOPIFY_S, '', 0, domain);
}
});
}
UseShopifyCookiesOptions
- hasUserConsent
If set to `false`, Shopify cookies will be removed. If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year. Defaults to false.
boolean
- domain
The domain scope of the cookie. Defaults to empty string.
string
{
/**
* If set to `false`, Shopify cookies will be removed.
* If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.
* Defaults to false.
**/
hasUserConsent?: boolean;
/**
* The domain scope of the cookie. Defaults to empty string.
**/
domain?: string;
}
Was this section helpful?
Example code
import * as React from 'react';
import {useShopifyCookies} from '@shopify/hydrogen';
export default function App({Component, pageProps}) {
useShopifyCookies({hasUserConsent: false});
return <Component {...pageProps} />;
}
examples
Example code
description
I am the default example
JavaScript
import * as React from 'react'; import {useShopifyCookies} from '@shopify/hydrogen'; export default function App({Component, pageProps}) { useShopifyCookies({hasUserConsent: false}); return <Component {...pageProps} />; }
TypeScript
import * as React from 'react'; import {useShopifyCookies} from '@shopify/hydrogen'; export default function App({Component, pageProps}) { useShopifyCookies({hasUserConsent: false}); return <Component {...pageProps} />; }