### Import Core and Helper Modules in React Source: https://github.com/dfrankland/react-amphtml/blob/master/README.md Import all AMP elements and helper render props for actions and bindings. Also imports setup components for rendering pages. ```javascript import * as Amp from 'react-amphtml'; import * as AmpHelpers from 'react-amphtml/helpers'; import { AmpScripts, AmpScriptsManager, headerBoilerplate, } from 'react-amphtml/setup'; ``` -------------------------------- ### Build Complete AMP Page with React Source: https://context7.com/dfrankland/react-amphtml/llms.txt This example demonstrates how to build a complete, AMP-valid HTML document string using react-amphtml. It composes various AMP components and uses a two-pass rendering approach to manage scripts. ```tsx import React, { ReactElement } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import * as Amp from 'react-amphtml'; import * as AmpHelpers from 'react-amphtml/helpers'; import { AmpScripts, AmpScriptsManager, headerBoilerplate } from 'react-amphtml/setup'; function buildAmpPage(): string { const ampScripts = new AmpScripts(); const initialState = { heading: 'Welcome!', count: 0 }; const bodyContent = renderToStaticMarkup(
{/* State definition */} {initialState} {/* Bound heading */} {(props): ReactElement =>

{initialState.heading}

}
{/* Responsive image */} {/* Interactive button using Action */} {(props): ReactElement => ( )} {/* Form extension */}
, ); const html = renderToStaticMarkup( {headerBoilerplate('https://example.com/')} My AMP Site {ampScripts.getScriptElements()} {/* eslint-disable-next-line react/no-danger */} , ); return `${html}`; } const page = buildAmpPage(); // page is a complete, AMP-valid HTML document string // Includes: v0.js, amp-bind, amp-form scripts automatically ``` -------------------------------- ### Use Amp Helpers for Actions and Bindings in React Source: https://github.com/dfrankland/react-amphtml/blob/master/README.md Utilize render prop components from 'react-amphtml/helpers' to add AMP attribute directives for actions and bindings. This example shows attaching actions to a button and using state with bindings for dynamic content. ```javascript import * as Amp from 'react-amphtml'; import * as AmpHelpers from 'react-amphtml/helpers'; // Example of attaching actions to elements {(props) => ( )} // Example of using state and bindings together const defaultHeading = { text: 'Hello, World!', }; // ... {defaultHeading} {(props): ReactElement =>

{defaultHeading.text}

}
``` -------------------------------- ### Set up AMP HTML Pages with Boilerplate and Scripts Source: https://github.com/dfrankland/react-amphtml/blob/master/README.md Use components from 'react-amphtml/setup' to generate AMP HTML pages. This includes managing AMP scripts and inserting required boilerplate markup for AMP validation. ```javascript import * as Amp from 'react-amphtml'; import { AmpScripts, AmpScriptsManager, headerBoilerplate, } from 'react-amphtml/setup'; const ampScripts = new AmpScripts(); const bodyContent = renderToStaticMarkup(
, ); /* eslint-disable react/no-danger */ const html = renderToStaticMarkup( {headerBoilerplate('/')} react-amphtml {ampScripts.getScriptElements()} , ); /* eslint-enable */ const htmlPage = ` ${html} `; ``` -------------------------------- ### Render AMP Components with React AmpHTML Source: https://context7.com/dfrankland/react-amphtml/llms.txt Demonstrates rendering built-in and extension AMP components, including managing script registration for extensions. Ensure AmpScriptsManager wraps content and use headerBoilerplate for canonical links and boilerplate styles. ```tsx import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import * as Amp from 'react-amphtml'; import { AmpScripts, AmpScriptsManager, headerBoilerplate } from 'react-amphtml/setup'; const ampScripts = new AmpScripts(); // Render body content — AmpScriptsManager collects required extension scripts const bodyContent = renderToStaticMarkup(
{/* Built-in: no extra script tag generated */} {/* Extension: amp-accordion script auto-registered */}

Section 1

Content for section 1.

{/* Extension with explicit version */} Hello, {'{{name}}'}!
, ); // Assemble full AMP page const html = renderToStaticMarkup( {headerBoilerplate('https://example.com/page')} My AMP Page {ampScripts.getScriptElements()} , ); const fullPage = `${html}`; // fullPage is a fully valid AMP HTML document ``` -------------------------------- ### Generate AMP Head Boilerplate Source: https://context7.com/dfrankland/react-amphtml/llms.txt Use `headerBoilerplate` to generate essential AMP `` elements. Pass the canonical URL of the page as the sole argument. ```tsx import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import * as Amp from 'react-amphtml'; import { AmpScripts, headerBoilerplate } from 'react-amphtml/setup'; const ampScripts = new AmpScripts(); const head = renderToStaticMarkup( {headerBoilerplate('https://example.com/my-amp-page')} My AMP Page {ampScripts.getScriptElements()} , ); // Produces: // // // // // // // My AMP Page // // ``` -------------------------------- ### `Action` Helper Source: https://context7.com/dfrankland/react-amphtml/llms.txt The `Action` render-prop component simplifies the creation of AMP event-driven actions by compiling an `on` attribute string. It maps event names to arrays of action strings and injects the attribute into the child element. ```APIDOC ## `Action` Helper — `import * as AmpHelpers from 'react-amphtml/helpers'` `Action` is a render-prop component that generates the AMP `on` attribute string for event-driven actions. It accepts an `events` object mapping event names (e.g., `tap`, `change`, `submit`) to arrays of action strings, and injects the compiled `on="..."` attribute into the child element via the render prop callback. ```tsx import React, { ReactElement } from 'react'; import * as AmpHelpers from 'react-amphtml/helpers'; import { ActionOnProps } from 'react-amphtml/helpers'; // Single event, multiple actions const tapButton = ( {(props: ActionOnProps): ReactElement => ( )} ); // Renders: )} )} ); ``` ``` -------------------------------- ### Use Amp Components in React Source: https://github.com/dfrankland/react-amphtml/blob/master/README.md Import and use AMP HTML directives as React components. This includes custom 'amp-*' elements and standard HTML elements validated by AMP. ```javascript import * as Amp from 'react-amphtml'; // ... ``` -------------------------------- ### AmpState Component Source: https://context7.com/dfrankland/react-amphtml/llms.txt The AmpState component renders an `` element, allowing you to manage inline JSON state or load state from a remote URL. It automatically registers the `amp-bind` extension. ```APIDOC ## AmpState Component `AmpState` renders an `` element and automatically registers the `amp-bind` extension script via the `AmpScriptsManager` context. Accepts either an inline JavaScript object as `children` (serialized as JSON) or a remote `src` URL for lazy-loaded state. The `id` prop is used to reference the state in `data-amp-bind-*` expressions. ```tsx import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import * as Amp from 'react-amphtml'; import { AmpScripts, AmpScriptsManager } from 'react-amphtml/setup'; const ampScripts = new AmpScripts(); const output = renderToStaticMarkup(
{/* Inline JSON state */} {{ name: 'Alice', loggedIn: true }} {/* Remote state loaded from URL */}
, ); // Rendered output: // // // // // amp-bind script is automatically registered: console.log(ampScripts.getScripts().map(s => s.src)); // ['https://cdn.ampproject.org/v0.js', 'https://cdn.ampproject.org/v0/amp-bind-latest.js'] ``` ``` -------------------------------- ### `headerBoilerplate` Function Source: https://context7.com/dfrankland/react-amphtml/llms.txt The `headerBoilerplate` function generates essential AMP `` elements, including meta tags, canonical link, and AMP boilerplate styles. It requires the canonical URL of the page as its only argument. ```APIDOC ## `headerBoilerplate` Function — `import { headerBoilerplate } from 'react-amphtml/setup'` `headerBoilerplate` returns an array of required AMP `` elements: a UTF-8 charset ``, a viewport ``, a `` elements or raw script source URLs. It always includes the base AMP runtime script (`v0.js` or `amp4ads-v0.js`). ```tsx import { AmpScripts } from 'react-amphtml/setup'; // Default: AMP format (includes v0.js) const ampScripts = new AmpScripts(); // After rendering components into AmpScriptsManager, retrieve scripts: // As React elements (ready to embed in ): const scriptElements = ampScripts.getScriptElements(); // Returns ReactElement[] of //
// // amp-bind script is automatically registered: console.log(ampScripts.getScripts().map(s => s.src)); // ['https://cdn.ampproject.org/v0.js', 'https://cdn.ampproject.org/v0/amp-bind-latest.js'] ``` -------------------------------- ### AmpScripts Class for Managing AMP Extension Scripts Source: https://context7.com/dfrankland/react-amphtml/llms.txt The AmpScripts class collects required AMP extension scripts. Instantiate it once per page render and pass it to AmpScriptsManager. Retrieve script elements or source URLs after rendering. ```tsx import { AmpScripts } from 'react-amphtml/setup'; // Default: AMP format (includes v0.js) const ampScripts = new AmpScripts(); // After rendering components into AmpScriptsManager, retrieve scripts: // As React elements (ready to embed in ): const scriptElements = ampScripts.getScriptElements(); // Returns ReactElement[] of