Best practices for handling the re-rendering of 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:

WebViewer React Custom Element

1const CustomItem = ({
2 borderColor = "black",
3 bgColor = "lightgray",
4 text = "Hello, Custom Element!",
5}) => {
6 return (
7 <div
8 style={{
9 border: `1px solid ${borderColor}`,
10 padding: "10px",
11 backgroundColor: bgColor,
12 width: "200px",
13 height: "50px",
14 }}
15 >
16 {text}
17 </div>
18 );
19};
20
21// Then in your WebViewer Code
22const [text, setText] = useState("Initial Text");
23const [borderColor, setBorderColor] = useState("black");
24
25const newCustomElement = {
26 type: 'customElement',
27 dataElement: 'customElementButton',
28 render: () => <CustomItem text={text} borderColor={borderColor} bgColor="lightgray" />
29};
30
31instance.UI.setHeaderItems(header => {
32 header.push(newCustomElement);
33});

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 and a Custom Button 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.

React Sample with Custom Element

1const CustomItem = ({
2 borderColor = "black",
3 bgColor = "lightgray",
4 text = "Hello, Custom Element!",
5}) => {
6 return (
7 <div
8 style={{
9 border: `1px solid ${borderColor}`,
10 padding: "10px",
11 backgroundColor: bgColor,
12 width: "200px",
13 height: "50px",
14 }}
15 >
16 {text}
17 </div>
18 );
19};
20
21const App = () => {
22 const viewer = useRef(null);
23 const [text, setText] = useState("Initial Text");
24 const [borderColor, setBorderColor] = useState("black");
25 const [viewerInstance, setViewerInstance] = useState(null);
26
27 useEffect(() => {
28 WebViewer(
29 {
30 path: '/webviewer/lib',
31 initialDoc: '/files/PDFTRON_about.pdf',
32 licenseKey: 'your_license_key', // sign up to get a free trial key at https://dev.apryse.com
33 },
34 viewer.current,
35 ).then((instance) => {
36 setViewerInstance(instance);
37 });
38 }, []);
39
40 const changetTextHandler = () => {
41 setText(`Updated: ${Math.random()}`);
42 }
43
44 useEffect(() => {
45 if (viewerInstance) {
46 const customItem = new viewerInstance.UI.Components.CustomElement({
47 dataElement: 'myCustomElement',
48 className: 'my-custom-element',
49 style: {
50 border: '1px solid black'
51 },
52 render: () => <CustomItem text={text} borderColor={borderColor} bgColor="lightgray" />,
53 });
54
55 const topHeader = new viewerInstance.UI.Components.ModularHeader({
56 dataElement: 'default-top-header',
57 placement: 'top',
58 grow: 0,
59 gap: 12,
60 position: 'start',
61 stroke: true,
62 dimension: {
63 paddingTop: 8,
64 paddingBottom: 8,
65 borderWidth: 1,
66 },
67 style: {},
68 items: [customItem],
69 })
70
71 viewerInstance.UI.setModularHeaders([topHeader])
72 }
73
74 }, [viewerInstance, text, borderColor]);
75
76 return (
77 <div className="App">
78 <button onClick={changetTextHandler}>Change Text</button>
79 <button onClick={() => setBorderColor("red")}>Change Border</button>
80 <div className="header">React sample</div>
81 <div className="webviewer" ref={viewer}></div>
82 </div>
83 );
84};
85
86export default App;

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.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales