Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop of the 'componentDidUpdate' method (caused by an infinite state update). #250

Open
Gianarci opened this issue Oct 3, 2022 · 1 comment

Comments

@Gianarci
Copy link

Gianarci commented Oct 3, 2022

Hi,

I've tried to render a list with a multiple items (of fixed size) per row, but it always prints 'ReactList failed to reach a stable state' in the console and slowdown the rendering. I've tried with both flex-box and inline-block, but no success. For e.g. the following fails:

.item {
    display: inline-block;
    width: 300px;
    height: 420px;
}

const itemsRenderer = (items: JSX.Element[], ref: string) => <div ref={ref as any}>{items}</div>;

const itemRenderer = (i: number) => <div key={`control_${i}`} className='item'>test</div>;

<ReactList itemRenderer={itemRenderer} itemsRenderer={itemsRenderer} length={500} type='uniform' />

Can anyone show me a correct and stable way to render a list with multiple items per row? Thanks in advance.

P.S.: If I change 'inline-block' to 'block' the message disappears.

@Gianarci Gianarci changed the title Multiple elements per row ('uniform' type), doesn't works. Multiple elements per row ('uniform' type), doesn't work. Oct 3, 2022
@Gianarci
Copy link
Author

Gianarci commented Oct 3, 2022

Ok, looking at the source, the issue is the following:

the componentDidUpdate invokes the updateFrame. That method invokes maybeSetState that updates the component's state (if the state is changed). When the state of a component is updated, the componentDidUpdate is invoked again. You might think that in such new call of componentDidUpdate, the maybeSetState doesn't update the state anymore but debugging the maybeSetState, I've seen that the state is not updated and so the code changes it again leading to an infinite loop (this behavious is strange because in theory React should guarantee that the state is updated before invoking componentDidUpdate). I use React 18.2.0. This happens always when I scroll down the list and I reach the last element on the bottom.

@Gianarci Gianarci changed the title Multiple elements per row ('uniform' type), doesn't work. Infinite loop of the 'componentDidUpdate' method (caused by an infinite state update). Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant