useCMSContext: Accessing Dynamic Page Data
Best Practices Guide for CMS Integration
Introduction
The useCMSContext hook is the primary way to access the current page's metadata and content within an Archibald application. It provides a reactive link to the CMS data fetched during the routing phase.
How it works
- Context Subscription: The hook connects to the global CMS context provider.
- Data Retrieval: It extracts the
pageobject, which contains all the structured content from the CMS for the current URL. - Reactivity: When the user navigates to a new page, the context is updated, and all components using this hook re-render with the new page data.
Why use useCMSContext?
- Deep Access: Access page-level data (like SEO titles, breadcrumbs, or header settings) from anywhere in your component tree without prop drilling.
- Consistent Content: Ensures that all components are rendering data from the same source of truth.
See Also
For a detailed technical breakdown and additional implementation patterns, refer to the following resources:
Key Takeaways
- Use it for "Global" Page Elements: Ideal for components like Breadcrumbs, Page Titles, or Sidebars that need data from the current page regardless of where they are placed.
- Check for
pagepresence: While usually available on CMS-routed pages, always ensure thepageobject exists before accessing its properties to avoid runtime errors. - Keep it Read-Only: Treat the data returned by
useCMSContextas read-only. If you need to modify page-level state locally, use a separateuseStatehook. - Combine with
usePreviewTicket: For editors, this hook works seamlessly with Archibald's preview system, showing draft content automatically.