# useRender

> Builds a part that supports the render prop and state data attributes.

`useRender` renders an element that callers can replace or compose through a `render` prop, the same way Base UI parts work. It is Base UI's [`useRender`](https://base-ui.com/react/utils/use-render) re-exported through a Dify UI subpath.

```tsx
import { useRender } from '@langgenius/dify-ui/use-render'
```

## Usage guidelines

- **Use it for a Dify-authored part with no Base UI counterpart**: a part that renders a plain element, such as a link or a card entry, gets the same `render` composition as the rest of the library.
- **Do not wrap a Base UI part with it**: Base UI parts already accept `render`. Wrap the part directly and forward its props.
- **Do not rebuild an existing component**: when a Dify UI component already covers the element, compose it instead of building a look-alike with `useRender`.
- **Expose state through `state`**: each state property becomes a `data-*` attribute on the element and the second argument of a `render` callback, so callers style the state with data-attribute variants rather than reading it in React.
- **Type the props with `useRender.ComponentProps`**: it adds the `render` prop to the element's own props.
- **Merge the part's own props with [`mergeProps`](/utilities/merge-props.md)**: the `props` parameter is spread on the element and merged with the `render` element's props.

## Parameters and composition types

```tsx
interface UseRenderParameters<
  State,
  RenderedElementType extends Element,
  Enabled extends boolean | undefined,
> {
  /**
   * The React element or a function that returns one to override the default element.
   */
  render?: UseRenderRenderProp<State> | undefined
  /**
   * The ref to apply to the rendered element.
   */
  ref?: React.Ref<RenderedElementType> | React.Ref<RenderedElementType>[] | undefined
  /**
   * The state of the component, passed as the second argument to the `render` callback.
   * State properties are automatically converted to data-* attributes.
   */
  state?: State | undefined
  /**
   * Custom mapping for converting state properties to data-* attributes.
   * @example
   * { isActive: (value) => (value ? { 'data-is-active': '' } : null) }
   */
  stateAttributesMapping?: StateAttributesMapping<State> | undefined
  /**
   * Props to be spread on the rendered element.
   * They are merged with the internal props of the component, so that event handlers
   * are merged, `className` strings and `style` properties are joined, while other external props overwrite the
   * internal ones.
   */
  props?: Record<string, unknown> | undefined
  /**
   * If `false`, the hook will skip most of its internal logic and return `null`.
   * This is useful for rendering a component conditionally.
   * @default true
   */
  enabled?: Enabled | undefined
  /**
   * The default tag name to use for the rendered element when `render` is not provided.
   * @default 'div'
   */
  defaultTagName?: keyof React.JSX.IntrinsicElements | undefined
}

type UseRenderReturnValue<Enabled extends boolean | undefined> = Enabled extends false
  ? null
  : React.ReactElement

type UseRenderComponentProps<
  ElementType extends React.ElementType,
  State = {},
  RenderFunctionProps = HTMLProps,
> = React.ComponentPropsWithRef<ElementType> & {
  render?: React.ReactElement | ComponentRenderFn<RenderFunctionProps, State> | undefined
}
```

`useRender.Parameters`, `useRender.ReturnValue` and `useRender.ComponentProps` are namespace aliases of `UseRenderParameters`, `UseRenderReturnValue` and `UseRenderComponentProps`.

## Examples

### Link part with state

The `external` state becomes a `data-external` attribute, and a caller can swap the anchor for a router link through `render`.

```tsx
import { cn } from '@langgenius/dify-ui/cn'
import { mergeProps } from '@langgenius/dify-ui/merge-props'
import { useRender } from '@langgenius/dify-ui/use-render'

type DocLinkState = {
  external: boolean
}

type DocLinkProps = useRender.ComponentProps<'a', DocLinkState>

function DocLink({ className, render, ...props }: DocLinkProps) {
  const external = props.target === '_blank'

  return useRender({
    defaultTagName: 'a',
    render,
    state: { external },
    props: mergeProps<'a'>(
      {
        className: cn('text-text-accent hover:underline data-external:pe-1', className),
        rel: external ? 'noopener noreferrer' : undefined,
      },
      props,
    ),
  })
}

const anchorLink = <DocLink href="/settings/members">Manage members</DocLink>
const routerLink = <DocLink render={<RouterLink to="/settings/members" />}>Manage members</DocLink>
```

## API reference

### useRender

Renders a Base UI element.

```ts
useRender<State extends Record<string, unknown>, RenderedElementType extends Element, Enabled extends undefined | boolean = undefined>(
  params: useRender.Parameters<State, RenderedElementType, Enabled>,
): UseRenderReturnValue<Enabled>
```

**Parameters**

| Parameter | Type                                                        | Description |
| --------- | ----------------------------------------------------------- | ----------- |
| `params`  | `useRender.Parameters<State, RenderedElementType, Enabled>` | —           |

**Returns:** `UseRenderReturnValue<Enabled>`
