Region React Context in the Storefront
Share the selected region across React components.
On this page
In this guide, you'll learn how to share the selected region in a React storefront.
Next.js App Router (recommended)
Do not keep the region in localStorage or a client context that fetches in useEffect.
On Next.js:
- The URL
[countryCode]is the customer's country. - Server modules call
getRegion(storeCode, countryCode). - Product list/retrieve pass
region_idso prices calculate. - Changing country is a navigation to a new
[countryCode], which updates the cart region viagetOrSetCart.
See Store Selected Region and Build a Next.js Storefront.
A client country switcher can still be a dropdown; it should router.push to the new country path, not write region_id to localStorage.
SPA: Region Context Provider
If you're building a client-rendered SPA, a context at the top of the tree can hold the selected region.
tsx
Copy
Add RegionProvider high in the tree. Child components call useRegion() for region and setRegion.