> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/get-started/faq/webviewer-custom-elements.md).

# Best practices for handling the re-rendering of Custom Elements

World's #1 PDF SDK Library for Web, Mobile, Server, Desktop WebViewer UI React Custom Elements

WebViewer exposes an API to render custom elements in the UI. The Custom Element component accepts a `render` function, which returns either a React element or an HTML element.

If your render returns a React component you may find that it does not update when its props change. Here is an example:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
const CustomItem = ({
  borderColor = "black",
  bgColor = "lightgray",
  text = "Hello, Custom Element!",
}) => {
  return (
    <div
      style={{
        border: `1px solid ${borderColor}`,
        padding: "10px",
        backgroundColor: bgColor,
        width: "200px",
        height: "50px",
      }}
    >
      {text}
    </div>
  );
};

// Then in your WebViewer Code
const [text, setText] = useState("Initial Text");
const [borderColor, setBorderColor] = useState("black");
  
const newCustomElement = {
    type: 'customElement',
    dataElement: 'customElementButton',
    render: () => <CustomItem text={text} borderColor={borderColor} bgColor="lightgray" />
};

instance.UI.setHeaderItems(header => {
    header.push(newCustomElement);
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

In the above example, anytime the state props `text` and `borderColor` change it will not trigger a re-render of your custom component.

The reason for this is that WebViewer renders your component as it was initially passed, and it will not update if its props change. If your UI requires this particular workflow you have several options.

The first is to consider if a custom element is the best fit for your use case. WebViewer also exposes a [Stateful Button ](/web/ui-customization/modular-ui/items.md#stateful-buttons)and a [Custom Button](/web/ui-customization/modular-ui/items.md#custom-buttons) that could achieve what you want.

Alternatively, you can structure your custom component to update each time the props change.

Here is a full example that shows how this could look in your React app.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
const CustomItem = ({
  borderColor = "black",
  bgColor = "lightgray",
  text = "Hello, Custom Element!",
}) => {
  return (
    <div
      style={{
        border: `1px solid ${borderColor}`,
        padding: "10px",
        backgroundColor: bgColor,
        width: "200px",
        height: "50px",
      }}
    >
      {text}
    </div>
  );
};

const App = () => {
  const viewer = useRef(null);
  const [text, setText] = useState("Initial Text");
  const [borderColor, setBorderColor] = useState("black");
  const [viewerInstance, setViewerInstance] = useState(null);

  useEffect(() => {
    WebViewer(
      {
        path: '/webviewer/lib',
        initialDoc: '/files/PDFTRON_about.pdf',
        licenseKey: 'your_license_key',  // sign up to get a free trial key at https://dev.apryse.com
      },
      viewer.current,
    ).then((instance) => {
      setViewerInstance(instance);
    });
  }, []);

  const changetTextHandler = () => {
    setText(`Updated: ${Math.random()}`);
  }

  useEffect(() => {
    if (viewerInstance) {
      const customItem = new viewerInstance.UI.Components.CustomElement({
        dataElement: 'myCustomElement',
        className: 'my-custom-element',
        style: {
          border: '1px solid black'
        },
        render: () => <CustomItem text={text} borderColor={borderColor} bgColor="lightgray" />,
      });

      const topHeader = new viewerInstance.UI.Components.ModularHeader({
        dataElement: 'default-top-header',
        placement: 'top',
        grow: 0,
        gap: 12,
        position: 'start',
        stroke: true,
        dimension: {
          paddingTop: 8,
          paddingBottom: 8,
          borderWidth: 1,
        },
        style: {},
        items: [customItem],
      })

      viewerInstance.UI.setModularHeaders([topHeader])
    }

  }, [viewerInstance, text, borderColor]);

  return (
    <div className="App">
      <button onClick={changetTextHandler}>Change Text</button>
      <button onClick={() => setBorderColor("red")}>Change Border</button>
      <div className="header">React sample</div>
      <div className="webviewer" ref={viewer}></div>
    </div>
  );
};

export default App;
```

{% endcode %}
{% endtab %}
{% endtabs %}

The example above has a custom element that updates anytime the component's dependencies change. We achieve this by forcing a re-render of the custom element inside of a `useEffect`, where we re-insert it to the UI. Notice the dependency array of that hook has the two props, which is what triggers the re-render anytime they change.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/get-started/faq/webviewer-custom-elements.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
