Cells from components
A default cell shows its value as text. When a cell needs a badge, a progress bar or a button, put a framework component in the column's cellRenderer. When a cell needs its own editing UI, put one in cellEditor. Each wrapper has its own function.
| Framework | Renderer | Editor |
|---|---|---|
| React | reactCell(Component) | reactEditor(Component) |
| Vue 3 | vueCell(Component) | vueEditor(Component) |
| Angular | angularCell(Component) | angularEditor(Component) |
There is no live demo, because the docs site uses the renderer without a framework. The complete code for the same scenario (an employment-status badge and a component editor) in all three frameworks is on The same screen in React, Vue and Angular. Read the Framework guide first for what all three share.
Build a renderer component
This component draws employment status as a badge. When you put it in a column definition, wrap it in the function the wrapper provides.
import { Grid, reactCell, type CellRenderProps } from '@maxgrid/react';
function StatusBadge({ value }: CellRenderProps<Employee>) {
return (
<span className={value ? 'badge badge-on' : 'badge badge-off'}>
{value ? 'Active' : 'On leave'}
</span>
);
}
const columns = [
{ id: 'name', title: 'Name' },
{ id: 'active', title: 'Status', cellRenderer: reactCell(StatusBadge) },
];The component receives the cell context as props. Pass this columns array to <Grid options>.
This is the context the component receives. It is the same in all three frameworks.
| Name | Content |
|---|---|
value | The cell value |
row | The row object. Read it; do not modify it |
rowKey | The row key |
rowIndex | Position among the visible rows (after sort and filter) |
columnId | The column id |
text | The text after formatValue. Meant for accessible fallback text |
kind | The column kind (text, number ...) |
A column with a cellRenderer still sorts, filters and uses getValue as before. formatValue no longer decides the cell's content, and is used only for text.
Center content vertically in the cell
The cell is a plain block. Text sits in the middle through the row height, but anything that is not text (a progress bar, a chip, an icon) sticks to the top. The renderer owns vertical alignment. Create a div with the mxg-cell-center class inside the cell and draw into it: it lays children out in a row and centers them vertically. Do not put the class on the cell itself, because the grid rewrites the cell's className every time it refills the cell.
const box = document.createElement('div');
box.className = 'mxg-cell-center'; // display:flex; align-items:center; height:100%
el.append(box);
box.append(track, label);A renderer that only draws text does not need the box. For React, Vue and Angular components, give the same class to the root element.
How components are connected and their limits
The function that puts a component into a column does not return a ready-to-use renderer. It returns a marked factory. When <Grid> (<mx-grid>) passes over options.columns, it replaces that factory with a real one bound to that grid's context, and hands the result to core. This leads to a few limits.
- Component cells travel through
optionsonly. If you passreactCellstraight toapi.setColumns(...), it is not connected and the cell shows as an error. To change columns, build a newoptions. - For Angular, a column array built with
angularCelland passed to core directly, outside<mx-grid>, gives the same error. - Each grid makes one factory per component. If
optionsis rebuilt but the component is the same, cells are not recreated. Vue caches the result per column object and returns the same reference for the same input. - Two grids are independent. No global state is used.
Use context (Context and DI)
For a cell component to use the app's theme, translations or data client, the framework has to carry them down to the component. How far the wrapper carries them differs by framework.
Cell components are drawn through a portal at the <Grid> position in the React tree. Values from Providers above <Grid> (useContext, theme, i18n, query client) are visible as they are. Cells are not rendered with a separate createRoot, so you do not need to wrap them in Providers again.
<ThemeContext.Provider value="dark">
<Grid options={options} />
</ThemeContext.Provider>Components are reused while scrolling
Virtual scrolling creates DOM for only as many rows as are visible, and reuses the same DOM for other rows as you scroll. Components are treated the same way. One component instance is created per cell, and when another row enters that slot, the instance is not recreated. Only its inputs (props) change.
| When | What happens |
|---|---|
| A cell is filled for the first time | The component is created and attached (mount) |
| Scrolling puts another row into the slot | Only the same component's inputs change (update) |
| The same row is drawn again | Also an update (the row object may have changed) |
| The pool shrinks, columns change or the grid is destroyed | The component is removed (destroy) |
Even when you scroll through 100,000 rows quickly, the number of components created never exceeds the row pool size (the visible rows plus a margin) times the number of component columns. There is one thing to watch for.
State leaks into the next row
If a component keeps state, such as expanded or not, text being typed or optimistic UI, then after scrolling the previous row's state stays on the new row. A cell you expanded on row 1 appears expanded on row 40 after the scroll. Reset the state when rowKey changes.
Have the outer component render an inner component that uses rowKey as its key. When rowKey changes, React creates the inner component again and its state is cleared.
function Notes(props: CellRenderProps<Employee>) {
return <NotesBody key={props.rowKey} text={props.text} />;
}
function NotesBody({ text }: { text: string }) {
const [open, setOpen] = useState(false); // Starts fresh when the row changes.
return <button onClick={() => setOpen(!open)}>{open ? text : 'Expand'}</button>;
}
// { id: 'notes', cellRenderer: reactCell(Notes) }A component that only displays the value (a badge, a number format) reads value alone, so you need not worry. If a component holds state, a timer or a subscription, decide what to clean up each time a new row arrives. Timers and subscriptions are cleaned up only when the component is destroyed, never on an update.
Build an editor component
An editor appears over the cell when editing starts on a column with editable: true (Cell editing). The editor is created once per edit and is never reused. It is removed when the edit ends.
The component has three jobs.
| Job | Description |
|---|---|
| Report the current value | The grid reads it when confirming. How the value is read differs by framework (table below) |
| Show validation failures | If a validation rule fails, the grid hands you a message. The editor stays open |
| Take focus and report composition | It takes focus on start. While Korean text is being composed, confirm requests are ignored |
The grid itself handles Enter (confirm), Esc (cancel) and Tab (confirm and go to the next cell) in the editor area. The component does not need to handle keys. To finish by something other than a key, such as confirming the moment a select box changes, call commit() or cancel() to request it. The edit system decides what actually happens, so a request made during composition is ignored.
import { reactEditor, type CellEditorProps } from '@maxgrid/react';
function StatusEditor({ value, error, onChange, commit }: CellEditorProps<Employee>) {
return (
<select
defaultValue={value ? 'on' : 'off'}
aria-invalid={error !== null}
onChange={(event) => {
onChange(event.target.value === 'on'); // Report the current value.
commit(); // Ask to confirm right after the choice.
}}
>
<option value="on">Active</option>
<option value="off">On leave</option>
</select>
);
}
// { id: 'active', editable: true, cellEditor: reactEditor(StatusEditor) }The props are value (the value when editing started), row, rowKey, rowIndex, columnId, trigger, startKey, error (the validation message or null), onChange(value), commit() and cancel(). The value the grid reads is the last value you passed to onChange, or the starting value if you never called it. So if you use an uncontrolled <input> and never call onChange, typing does not change the value. Focus goes to the first [data-mxg-autofocus], input, select, textarea or button inside the editor.
Here are the differences between the three at a glance.
| Item | React | Vue | Angular |
|---|---|---|---|
| Starting value | value prop | modelValue prop | value input |
| Current value | Last onChange value | Last modelValue, or getValue | getValue() (required) |
| Show validation failure | error prop | error prop | setError(message) |
| Request confirm or cancel | commit(), cancel() props | commit, cancel events | commit, cancel inputs (functions) |
| Focus | First input element | Expose focus, else first input element | focus(), else first input element |
| Composition (IME) | Tracked by the wrapper | Tracked by the wrapper; exposed isComposing wins | Tracked by the wrapper; isComposing() wins |
The edit system keeps Enter during Korean composition from confirming the edit. Even if the component does not implement isComposing, the wrapper learns whether composition is running from compositionstart and compositionend on the editor area. How smoothly composition carries into the input when a first keystroke starts the edit differs by browser (Cell editing).
A component exception does not stop the grid
If a component throws while rendering, only that cell shows an error. The grid and the other cells keep working.
- The error cell gets the text
⚠ 오류(⚠ Errorwithlocale: 'en') and themxg-cell-errorclass. - An
errorevent arrives (code: 'callback-error'), withwheresaying where it happened, such ascellRenderer(columnId). An error at the same place is reported once per update, and also goes toconsole.error, so it shows even with no listener. - The next time that slot is filled (scroll, data refresh), the component is created again from scratch. One exception does not break the cell for good.
- React puts an error boundary around each cell and retries on the next props change. You can also receive the exception through
<Grid onComponentError={(error) => ...}>; without it, it goes toconsole.error. - In Vue, the root component's
onErrorCapturedcatches exceptions from the setup, render and hooks of descendants. - A component that calls
api.destroy()is also handled safely. No component calls happen afterward.
If an editor component's getValue throws, the grid treats it as a validation failure instead of cancelling the confirm.
Pitfalls
- Pass component cells through
options.columnsonly. Passing them straight toapi.setColumns()gives an error cell (How components are connected and their limits). - To use an editor, the column also needs
editable: true. AcellEditoralone does not start editing. Adateorcustomcolumn can be edited too once you give it acellEditorandeditable. - As many component cells stay alive as the row pool size times the number of component columns. If you turn every column into a component, you get that many components. Columns that only show text are lighter as default cells.
- When a button inside a cell must change a row, the context does not carry a
GridApi. Keep theGridApiin the parent that uses the component and pass it down through props, inputs orprovide. - A cell component can insert an HTML string by itself, for example through
innerHTML, and the grid cannot stop it. Use the framework's templates, which escape automatically, and never insert user input as HTML. - The
rowobject is the grid's own original. Do not writerow.name = ...in a component. Use editing orapi.updateRows.