Skip to content

Commit

Permalink
docs(headless commerce react samples): add note regarding context + u…
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeaudoincoveo authored Aug 26, 2024
1 parent d7d72a6 commit 8790fbb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default function CartPage(props: ICartPageProps) {
});

useEffect(() => {
/**
* It is important to call the `Context` controller's `setView` method with the current URL when a page is loaded,
* as the Commerce API requires this information to function properly.
*/
contextController.setView({url});

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export default function HomePage(props: IHomePageProps) {
});

useEffect(() => {
/**
* It is important to call the `Context` controller's `setView` method with the current URL when a page is loaded,
* as the Commerce API requires this information to function properly.
*/
contextController.setView({url});

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default function ProductDescriptionPage(
const product = loadProduct();

useEffect(() => {
/**
* It is important to call the `Context` controller's `setView` method with the current URL when a page is loaded,
* as the Commerce API requires this information to function properly.
*/
contextController.setView({url});
}, [contextController, url]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export default function ProductListingPage(props: IProductListingPageProps) {
}, [productListingController]);

useEffect(() => {
/**
* It is important to call the `Context` controller's `setView` method with the current URL when a page is loaded,
* as the Commerce API requires this information to function properly.
*
* Note, however, that calling this method will reset the query, pagination, sort, and facets.
*
* This means that on a search or listing page, you must call this method BEFORE you bind the URL manager.
* Otherwise, the URL manager will restore the state from the URL parameters, and then this state will get
* immediately reset when the `setView` method is called.
*/
contextController.setView({url});
const unsubscribe = bindUrlManager();

Expand Down
10 changes: 10 additions & 0 deletions packages/samples/headless-commerce-react/src/pages/search-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export default function Search(props: ISearchProps) {
}, [searchController]);

useEffect(() => {
/**
* It is important to call the `Context` controller's `setView` method with the current URL when a page is loaded,
* as the Commerce API requires this information to function properly.
*
* Note, however, that calling this method will reset the query, pagination, sort, and facets.
*
* This means that on a search or listing page, you must call this method BEFORE you bind the URL manager.
* Otherwise, the URL manager will restore the state from the URL parameters, and then this state will get
* immediately reset when the `setView` method is called.
*/
contextController.setView({url});
const unsubscribe = bindUrlManager();

Expand Down

0 comments on commit 8790fbb

Please sign in to comment.