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 usePage 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
): 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

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)
})