Skip to content

Commit 2fff1fe

Browse files
docs: component renderer update function (#5412) (#5415)
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent 750b9ca commit 2fff1fe

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

articles/components/grid/renderers.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,33 @@ grid.addColumn(
217217
() -> VaadinIcon.ARROW_LEFT.create()));
218218
----
219219

220+
Component renderers allow providing an optional update function that can be used to optimize rendering.
221+
Without an update function, component renderers recreate components for each item whenever the grid needs to be refreshed.
222+
This has several downsides:
223+
224+
* It results in a large number of DOM changes in the browser as old elements are removed and new ones are added
225+
* It results in a larger payload that needs to be sent to the browser
226+
* Components lose their client-side state, for example, input fields lose their focus or entered text that has not been committed
227+
228+
By providing an update function, the grid can reuse existing components and only update their state. This results in fewer DOM changes, smaller payloads, and better user experience.
229+
230+
The following example shows how to provide an update function to a component renderer:
231+
232+
[source,java]
233+
----
234+
grid.addColumn(new ComponentRenderer<>(item -> {
235+
// Create the initial component using item name as text
236+
return new Badge(item.getName());
237+
}, (component, item) -> {
238+
// Update the component text when the item changes.
239+
// Cast is necessary as component renderer is typed
240+
// so that it allows the update function to return
241+
// a different component type if desired.
242+
((Badge) component).setText(item.getName());
243+
return component;
244+
}));
245+
----
246+
220247
You can create complex content for the grid cells by using the component APIs. This example is using [classname]`ComponentRenderer` to create complex content that listens for events and wraps multiple components in layouts:
221248

222249
[source,java]

0 commit comments

Comments
 (0)