Block
Generate, build, mount and handle React components at runtime with text descriptions.
<Block>
Client Component
Generates and mounts a React component at runtime based on a prompt
text description.
Live Example
useBlock
hook
useBlock
is a utility hook that allows for access to similar features as Block
.
Setup
Add block: handleBlockRequest()
to the API route handler.
API Reference
Types
BlockOptions
Block API route handler options.Properties
openAiApiKeystring
- default`process.env.OPENAI_API_KEY`.
BlockRequestBodyinterface
Block request body.Properties
promptstring
A text description of the desired component.- example'A footer with copyright for this year with the company name Acme'
BlockResultinterface
Block result after processing.Properties
contentstring
Stringified React components and declarations needed for data to be implemented by `usage`.- example`export const Wrapper = ({children,id}) => <div id={id} className='p-5'>{children}</div> export const List = ({data}) => <ul> {data?.map(item => (<li key={item.id} className='text-sm'>{item.text}</li>))} </ul> `
- notethat this is just a raw string containing React components. It will need to be sanitized and parsed before usage.
usagestring
Stringified React components with default props implementation of `content`.- example`<Wrapper id="main"> <List data={[{id:1,text:'Hello'},{id:2,text:'World'}]}/> </Wrapper> `
- notethat this is just a raw string containing React components. It will need to be sanitized and parsed before usage.
ChatGptCompletionResponse
The response body for `getBlock`.Properties
responseTextstring
The response text extracted from the completion message content.tokensUsednumber
Total number of tokens used in the request (prompt + completion).finishReasonstring
The reason the chat stopped. This will be `stop` if the model hit a natural stop point or a provided stop sequence, `length` if the maximum number of tokens specified in the request was reached, `content_filter` if content was omitted due to a flag from our content filters, `tool_calls` if the model called a tool, or `function_call` (deprecated) if the model called a function.errorMessagestring
The error message if there was an error.Components
BlockPropsinterface<object>
Props to pass to the `<Block>` Client Component.Properties
promptstring
A text description of the desired component.- example'A footer with copyright for this year with the company name Acme'
fallbackReactElement
A React tree to render in the error boundary if the `<Block>` build or renderer fails.loadingReactNode
A React tree to render in the target root before rendering the `<Block>`.errorBoundaryPropsOmit<ErrorBoundaryPropsWithFallback, string>
Props to pass to the wrapping error boundary.Blockfunction
Block is a client component that renders a generated React component based on a `prompt`.Parameters
propsBlockPropsrequired
- linkBlockProps
Hooks
useBlockfunction
Generates a React component based on a `prompt`. Uses `createRoot` to mount the component to a browser DOM element assigned with the `id`.Parameters
promptstringrequired
A text description of the desired component.- example'A footer with copyright for this year with the company name Acme'
onErrorfunction
Callback when errors are thrown. Used to e.g show an error boundary.- example`(error) => showBoundary(error)`
Parameters
errorrequired
The error object thrown by the Block.Returns
idstring
`id` used to attach to the DOM element to mount the component in.loadBlockfunction
Loads the block content into the DOM element with the `id`.Utilities
getBlockfunction
Generates code for a React component based on the provided `BlockRequestBody`. Server Action that calls the third-party API directly on the server. This avoids calling the Next.js API route handler allowing for performant Server Components.Parameters
requestBlockRequestBodyrequired
- linkBlockRequestBody
optionsBlockOptions
- linkBlockOptions
fetchBlockfunction
Generates code for a React component based on the provided `BlockRequestBody`. Client-side fetch handler that calls the internal Next.js API route handler, then the third-party API. Best used for Client Components and functionality.Parameters
bodyBlockRequestBodyrequired
- linkBlockRequestBody
configRequestConfigOnly
Fetch utility request options without the `body`fetchProcessedBlockfunction
Generates the code for a React component based on the provided `BlockRequestBody` and processes the response then mounts the component in the DOM element with the specified `id`. Uses `fetchBlock` under the hood to generate and fetch the block, then uses `esm.sh/build` to process the response and mount the component. Client-side fetch handler that calls the internal Next.js API route handler, then the third-party API. Best used for Client Components and functionality.Parameters
propsrequired
- linkBlockRequestBody
configRequestConfigOnly
Fetch utility request options without the `body`