Search code examples
javascriptreactjstypescriptreact-dndreact-data-grid

Error on implementing react-data-grid 7.0.0 from TS to React


I'm trying to implement DnD functionality in react-data-grid, but I'm getting "TypeError: Object(...) is not a function" Error.

There is a same file in TypeSript that I provided in the sandbox(it is only for reference). I'm trying to implement the functionality in React and something is wrong when converting TS code to React code. I'm using react-data-grid ^7.0.0-canary.33.

                    <DndProvider backend={HTML5Backend}>
                      <DataGrid
                        rowRenderer={p => <DraggableRowRenderer {...p} onRowReorder={onRowReorder} />}                  
                      />
                    </DndProvider>
// this is the component where the error is. I think I'm doing something wrong when typing react code(the orignal implementation is in TS)

import { useDrag, useDrop } from 'react-dnd';
import { Row } from 'react-data-grid';
import useCombinedRefs from 'react-data-grid';
// import { row } from 'react-dnd'
import clsx from 'clsx';
import './DraggableRowRenderer.less';

export default function DraggableRowRenderer({
  rowIdx,
  isRowSelected,
  className,
  onRowReorder,
  ...props}) {
  const [{ isDragging }, drag] = useDrag({
    item: { index: rowIdx, type: 'ROW_DRAG' },
    collect: monitor => ({
      isDragging: monitor.isDragging()
    })
  });

  const [{ isOver }, drop] = useDrop({
    accept: 'ROW_DRAG',
    drop({ index, type }) {
      if (type === 'ROW_DRAG') {
        onRowReorder(index, rowIdx);
      }
    },
    collect: monitor => ({
      isOver: monitor.isOver(),
      canDrop: monitor.canDrop()
    })
  });

  className = clsx(
    className,
    {
      'rdg-row-dragging': isDragging,
      'rdg-row-over': isOver
    }
  );

  return (
    <Row
      ref={useCombinedRefs(drag, drop)}
      rowIdx={rowIdx}
      isRowSelected={isRowSelected}
      className={className}
      {...props}
    />
  );
}


Solution

  • I've got what the issue was. UseCombinedRef was not exported from the library. So what I did is I'm copying the internal function in my file. function useCombinedRefs(...refs) { return useCallback(handle => { for (const ref of refs) { if (typeof ref === 'function') { ref(handle); } else if (ref !== null) { ref.current = handle; } } }, refs); }

    Just paste this function instead of importing it and you will be good to go. Thanks to @drag13 for solving this issue. Duplicate issue here. Sandbox link here. Library Issue link here.