Skip to content

Table re-renders many times when using React.useDeferredValue() #6030

@Bram-Zijp

Description

@Bram-Zijp

TanStack Table version

8.21.3

Framework/Library version

18.3.1

Describe the bug and the steps to reproduce it

In my project we use React and it's useDeferredValue hook for a piece of state re-used in about 11 layers of the application. The hook in question is used to mutate configuration and is also used as base to create column definitions. The Queue mechanism inside the createTable the function is conflicting with useDeferredValue and causing many re-renders for about 4-8 seconds. It seems to cause a re-render in the table for about (2*11) 22 times where 11 is the amount of components using the hook with the deferred state. I can't reproduce it in code sandbox likely because the private application where it's reproducible is a lot bigger compared to what one would commonly write in a sandbox.

Workaround
When I remove the useDeferredValue usage the re-render issue is gone.
When I disable autoResetPageIndex the re-render issue is also gone as Tanstack table won't use the queue mechanism. It's worth noting I am not even using pagination.

The culprit seems to be in this code. I logged the queued array in the while loop and every 50-100 ms it would log an array with a single callback in it.

    _queue: cb => {
      queued.push(cb)

      if (!queuedTimeout) {
        queuedTimeout = true

        // Schedule a microtask to run the queued callbacks after
        // the current call stack (render, etc) has finished.
        Promise.resolve()
          .then(() => {
            while (queued.length) {
              queued.shift()!()
            }
            queuedTimeout = false
          })
          .catch(error =>
            setTimeout(() => {
              throw error
            })
          )
      }
    },

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

.

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

None

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions