Variations of this question has been asked at least a couple of times before. For example:
React component in a Tabulator custom formatter and
Properly display React component in react-tabulator column
Apologies for posting a similar question, but I have additional questions to ask.
For illustrative purposes, my React component is simple and looks like this:
type ReactThingProps = {
chldren: any;
colour: string;
};
const ReactThing: React.FunctionComponent<ReactThingProps> = ({
colour,
children,
}: ReactThingProps) => {
return <span style={{ backgroundColor: colour }}>{children}</span>;
};
export default ReactThing;
My React based formatter currently looks like this:
const ReactFormatter = (
cell: any,
_formatterParams: object,
_onRendered: EmptyCallback
) => {
const reactNode = document.createElement("span");
const reactRoot = createRoot(reactNode);
reactRoot.render(
<ReactThing colour={cell.getValue()}>{cell.getValue()}</ReactThing>
);
return reactNode;
};
And I hook it up like any other formatter, in the column definition:
{ title: "Despised Color", field: "despCol", formatter: ReactFormatter },
While what I have "works", I feel it is far from optimal.
Problem #1: I'm creating a root in every cell of the column, and rendering react into it. I think I should be calling unmount on reactRoot, as I think cells could get destroyed as a result of the grid's virtual dom. Question: What event could I hook into to clean up my react root code?
Problem #2: Assuming I can clean up the dom node successfully, it feels like it's probably a lot of overhead in creating the root, rendering to it, and tearing it down when the cell is about to get destroyed. Question: Is there a less naive way to introduce React components into a custom formatter (or some other extension point)?