React-Table re-renders entire table on select

喜欢而已 提交于 2021-02-08 04:42:34

问题


I have a relatively standard react-table with rows that can be selected- implemented using the HOC SelectTable, much like the example react-table provides:

https://github.com/tannerlinsley/react-table/blob/v6/docs/src/examples/selecttable/index.js

The key difference being one of my columns is a custom component:

 const columns = [
      {
      ...other columns...
      {
        Header: "Sound file",
        accessor: "sound_file",
        Cell: props => {
          return <SoundFilePlayer url={props.row.sound_file_url} />;
        },

        minWidth: 80
      }
    ];

With the SoundFilePlayer component being a react-player component that plays the sound file passed into the component as a prop. Essentially it loads the URL passed in and renders a sound player:

Before loading the url (with duration defaulting to 0:00)

Then rendering with correct duration once loaded:

The problem I'm facing is that whenever a row is selected in my table the entire table re-renders (which would be okay if there wasn't a sound player)- meaning the sound file player is reset back to 0:00 duration and then re-loaded once more with the correct duration. And this happens for every row.

My guess is that all the row's keys are checked to see if they are currently selected leading to a re-render of the entire table.

Is there anyway I only re-render only the selected row- as opposed to the entire table? I've looked at https://reactjs.org/docs/state-and-lifecycle.html as well as a few other SO posts but can't seem to only re-render the row affected. Or even just re-render the column with the checkbox?


回答1:


So I eventually found a solution - simply downgrading my react-table version, specifically to 6.8.6 fixed the issue.

It seems like a bug was introduced in later versions (which I doubt will be fixed, given that the attention is on version 7 now).




回答2:


You can try memoizing the component with useMemo hook like this

const file = React.useMemo(() => <SoundFilePlayer url={props.row.sound_file_url} />, [props.row.sound_file_url]);

It will won't recreate components function and will take it from memory unless file url changes.



来源:https://stackoverflow.com/questions/56806223/react-table-re-renders-entire-table-on-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!