question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MultiGrid doesn't re-render when data changed

See original GitHub issue

When using MultiGrid with non-zero fixedColumnCount, the content of the fixed column doesn’t get updated when needed. For example, when using with ArrowKeyStepper it doesn’t display focused cell. At the same time, the cell gets focused in the “main” grid.

Reproducible case

https://codesandbox.io/s/jp1rlqop9y

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

5reactions
groganmatt12commented, Oct 15, 2019

how would you do this for a functional component using hooks, or is this not possible with react pure components? i’ve made a virtualized Grid with hooks and functional components but it won’t re-render with state change.

3reactions
rhinoceraptorcommented, Mar 4, 2019

I think there is a better way than manually using forceUpdateGrids.

What is happening here is cell rendering control is being deferred to MultiGrid. MultiGrid and Grid are both PureComponents, so they will only update when their props change value/reference.

The component consuming MultiGrid probably has other props that its cells should update for, but MultiGrid doesn’t necessarily know that, it only updates when the props it has change.

One easy fix is to just spread the props from the component consuming MultiGrid. It’s definitely confusing, because MultiGrid will be passed props that it doesn’t need. But since the cell rendering control is being deferred, we have to ensure that MultiGrid will actually update when we want it to.

For example:

class MyGridComponent extends PureComponent {
  cellRenderer = ({ key, rowIndex, columnIndex }) => (
    <div key={key} className={this.props.className}>
      {`${rowIndex}, ${columnIndex}`}
    </div>
  )

  render = () => (
    <MultiGrid
      cellRenderer={this.cellRenderer}
      {/** ...other MultiGrid props... */}
  )
}

In this example, let’s say the props.className changed for MyGridComponent. The expected behavior is that each cell will get a new className. But className is not passed to MultiGrid, so it will not update (because it’s a PureComponent) and it won’t re-render the cells.

If we change render to pass the props, then the cells will be updated as expected:

  render = () => (
    <MultiGrid
      {...this.props /** Force MultiGrid to update when MyGridComponent updates */}
      cellRenderer={this.cellRenderer}
      {...other MultiGrid props...}
  )
Read more comments on GitHub >

github_iconTop Results From Across the Web

react-virtualized list item does not re-render with changed ...
In my use case, I allowed the user to sort the data, and the dataset needed to be passed to the MultiGrid so...
Read more >
react-virtualized list item does not re-render with changed ...
In my use case, I allowed the user to sort the data, and the dataset needed to be passed to the MultiGrid so...
Read more >
Just got resizable columns working on my react-virtualized ...
Resizable columns in a react-virtualized MultiGrid. This is a quick screen recording of my open-source software, D-Tale.
Read more >
How to Force-Refresh a React Child Component: The Easy Way
In the React world, forcing a re-render is frowned upon. ... When the user changes their color preference, the server writes new data...
Read more >
Rendering large lists with React Virtualized - LogRocket Blog
It manages the fetching of data as a user scrolls a List, Table, or Grid. MultiGrid. It decorates a Grid component to add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found