Skip to main content

fetchPage

The fetchPage function is useful when you want to retrieve the content of one page from outside the React context (where you could use the usePagePublic hook instead).

In particular, this comes in handy during the build process of a static website. Indeed, this is the method used in our Gatsby and Next.js starter projects.

Signature

const fetchPage = async (
slug: string,
apiKey: string,
language?: string
pageTypes?: types.IPageType[]
): Promise<types.Page>
PropertyDefinition
pageSlugThe slug (string) of the page to fetch.
apiKeyApi Key of your React Bricks app (a string).
language?The language of the desired translation for the page, if more than one share the same slug
pageTypes?The pageTypes used in React Bricks configuration. Useful only if you have external content (fetchPage cannot access them from the React context).

Return value

fetchPage returns a promise which resolves to a Page

Before using this page with ReactBricks' PageViewer component, you need to parse it with the cleanPage function.

Usage example

fetchPage('about-us', 'API_KEY').then((data) => {
const myPage = cleanPage(data, pageTypes, bricks)
console.log(myPage.content)
})