react-tabulator icon indicating copy to clipboard operation
react-tabulator copied to clipboard

onRef set to null in functional component after re-render

Open FlorianHockmann opened this issue 2 years ago • 4 comments

Environment Details

  • react-tabulator version: 0.18.1
  • OS: Ubuntu 20.04.4 LTS
  • Node.js version: v14.19.1

Long Description Version 0.18.1 changed the ref property to an onRef callback. We use ReactTabulator in a functional component. The callback method I provide to onRef gets called with a valid ref which looks good at first. But then our functional component gets re-rendered which resets the internal ref variable where I stored the ref we got from the callback to null. So, we don't have a valid ref anymore when we try to call a method on it.

Demo with code sandbox

Working version with 0.17.1 which still took a ref: https://codesandbox.io/s/react-tabulator-onref-workingwithref-r8nxd1

and then I tried to update it to 0.18.1 and use onRef instead: https://codesandbox.io/s/react-tabulator-onref-brokenwithonref-i6mwl1

The idea is that I want to handle selection of rows from outside the table. I did this for the simple example with two buttons. If you click them in the working example, then the rows get selected. With onRef this doesn't work however as the TableViewer gets re-rendered which makes the ref null again. That is why instead of selecting a row, the effect logs: tabulator invalid: null.

Workaround

Stay on 0.17.1 apparently.

FlorianHockmann avatar Apr 21 '22 08:04 FlorianHockmann

I am also facing same issue

RiteshParyali avatar May 03 '22 16:05 RiteshParyali

I have the same problem.

ajc-uconn avatar Aug 31 '22 17:08 ajc-uconn

I use this design pattern and it seems to work reliably:

const MyFuncComp = () => { const tableRef = React.useRef(null); <ReactTabulator onRef={(r) => (tableRef.current = r.current)} /> }

However, the following does not always work: <ReactTabulator onRef={(r) => (tableRef = r) } />

ScottTyson avatar Apr 25 '23 17:04 ScottTyson

For what it's worth, the above design pattern works using: "react-tabulator": "^0.18.1", "tabulator-tables": "^5.5.1", HOWEVER, I am getting console errors related to getComponent() calls.

I just updated to using: "react-tabulator": "^0.19.0", "tabulator-tables": "^5.5.2", The console errors are now gone.

ScottTyson avatar Sep 27 '23 17:09 ScottTyson