Examples › Using bodyRef with AutoAnimate

The bodyRef property gives you access to the underlying <tbody> element of the table. You can hand it over to the excellent AutoAnimate library to smoothly animate rows as they are added, removed or reordered. Use the buttons below the table to see it in action:

Name
Age
No records

AutoAnimate tracks rows by their DOM elements, which Mantine DataTable keys by record ID. Make sure each record has a unique, stable ID (see non-standard record IDs if your records don’t have an id property); otherwise reordered rows will simply be re-rendered in place, without animation.

Here is the code:

// ...
// React strict mode (Next.js dev) double-invokes ref callbacks on mount,
// which corrupts AutoAnimate's per-parent enabled state when using the
// official `useAutoAnimate` adapter. We sidestep it by calling `autoAnimate`
// directly, exactly once per real tbody mount. Production builds are unaffected.
const autoAnimateInitialized = useRef(false);
const bodyRef = useCallback((node: HTMLTableSectionElement | null) => {
  if (node && !autoAnimateInitialized.current) {
    autoAnimate(node);
    autoAnimateInitialized.current = true;
  }
}, []);

return (
  <>
    <DataTable
      borderRadius="sm"
      withTableBorder
      minHeight={200}
      columns={[
        {
          accessor: 'move',
          title: (
            <Center>
              <IconArrowsUpDown size={14} />
            </Center>
          ),
          render: (_, index) => (
            <Box display="flex">
              <ActionIcon
                variant="transparent"
                color="green"
                disabled={index === 0}
                onClick={() => moveUserUp(index)}
              >
                <IconArrowUp size={18} />
              </ActionIcon>
              <ActionIcon
                variant="transparent"
                color="green"
                disabled={index === records.length - 1}
                onClick={() => moveUserDown(index)}
              >
                <IconArrowDown size={18} />
              </ActionIcon>
            </Box>
          ),
        },
        { accessor: 'name', width: '100%' },
        { accessor: 'age', textAlign: 'right' },
        {
          accessor: 'delete',
          title: (
            <ActionIcon variant="transparent" color="red" disabled={records.length === 0} onClick={handleDeleteAll}>
              <IconTrashX size={18} />
            </ActionIcon>
          ),
          render: ({ id }) => (
            <ActionIcon variant="transparent" color="red" onClick={() => deleteUser(id)}>
              <IconTrash size={18} />
            </ActionIcon>
          ),
        },
      ]}
      records={records}
      bodyRef={bodyRef}
    />
    {/* ... */}
  </>
);

Head over to the next example to learn more.

Mantine DataTable is used and trusted by