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

[DataTable] Infinite loop in DataTable property onSelectionChanged

Open xdvarpunen opened this issue 10 years ago • 5 comments

Hi,

I have following problem: When I setState inside function I pass to onSelectionChanged, the function onSelectionChanged starts to call itself infinitely as loop.

rowSelectionChanged(data){
    this.setState({"rowsSelected": data});
}
onSelectionChanged={this.rowSelectionChanged.bind(this)}

Link to code pen to see the test: http://codepen.io/xdvarpunen/pen/LkykVK?editors=1011

Quote from Discord react-mdl room:

@asdf Sorry for the delay, I tested your codepen (thanks for that) and I also noticed the infinite loop. This is caused by this line :/ https://github.com/tleunen/react-mdl/blob/master/src/DataTable/Selectable.js#L61 I first did this because if the table receives new data, the selected rows might change as well, so I wanted to notify the "parent". But yep it's not the best idea actually 😒 Would you mind creating a ticket issue for this specific bug please? GitHub tleunen/react-mdl react-mdl - React Components for Material Design Lite

Maybe the solution would just be to check if the selected rows has changed before calling the callback

-XDVarpunen

xdvarpunen avatar Jul 01 '16 06:07 xdvarpunen

I'll try to make a bit more fixes and changes before releasing a new version early next week. Please be patient :)

tleunen avatar Jul 03 '16 22:07 tleunen

Thank you very much! Tyt (:

xdvarpunen avatar Jul 04 '16 04:07 xdvarpunen

New problem:

On first click the selection shows empty, On second click the selection shows correctly selected rows.

If click the select all box, it shows reverse choice: On first click it shows empty, On second click it shows all selected.

xdvarpunen avatar Jul 07 '16 06:07 xdvarpunen

You shared with me this codepen: http://codepen.io/xdvarpunen/pen/LkykVK?editors=1011

So you're trying to log the state right after setting it. This is the issue. setState is a synchronous operation, so sometimes you might not have the right info if you log just after setting it.

setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value. From: https://facebook.github.io/react/docs/component-api.html

tleunen avatar Jul 07 '16 12:07 tleunen

Are were certain that the issue is no longer exists? Been upgraded from 1.7.1 to 1.11.0. And the problem seems to come back. Working fine on 1.7.1 previously.

And now we are seeing it again.

Got this “Uncaught Invariant Violation: Maximum update depth exceeded” During Multiple Selection

More details: https://stackoverflow.com/questions/60608241/got-this-uncaught-invariant-violation-maximum-update-depth-exceeded-during-mu

padlock98 avatar Mar 18 '20 08:03 padlock98