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.

usage of ref with VariableSizeList does not re-render when ref.current is set

See original GitHub issue

I’m trying to use ref to set the scroll position when data is available. I believe this broke when upgrading to React 16.8.1.

Minimal example: https://codesandbox.io/s/8p7yy2xw2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
bvaughncommented, Feb 8, 2019

The logs show that your component renders twice (once with test false and once true) and your effect handler is called twice (once after each render). The first time it’s called, the list ref is null– because you don’t actually render the list when test is fail: screen shot 2019-02-08 at 1 29 50 pm

The next time it’s called, the ref points at the newly rendered list. 😄

1reaction
heyimalexcommented, Feb 13, 2019

There’s a difference between how renderChild in your example and rowRenderer are being used. In your sandbox. you call renderChild directly, which will execute in place and return a Child element. What’s returned from Parent’s render ultimately looks like this:

<div><Child num={someNumber} /></div>

If you check where react-window uses the children prop, you’ll see it’s using React.createElement and passing in rowRenderer as the type. If we update your sanbox to use React.createElement instead of calling the renderChild directly, what is returned from Parent’s render would look (roughly) like this:

<div><renderChild num={someNumber} /></div>

The big idea is that renderChild(props) and <renderChild {...props} /> look the same, but are handled very differently by React. One executes in place, just like any other function call; React doesn’t know about it because all it sees is what’s returned from render. The other hands off execution to React, and in doing so is treated as an actual component.

The kind of “aha” thing to remember is that <Component {...props} /> is syntactic sugar for React.createElement(Component, props), which ultimately becomes an object kinda like { type: Component, props: props }. Notice Component isn’t called, we just wrapped it up with the props we want it to be called with and return that. React knows what to do with it and takes care of calling it sometime later.

I hope I explained that alright!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to rerender when refs change - Stack Overflow
I want to load data when the drawControl not null. I check the document that may use callback ref. So, how do I...
Read more >
react-window
Examples. Fixed Size List; Variable Size List; Fixed Size Grid; Variable Size Grid; Scrolling indicators; Scrolling to an item; Memoized List items ...
Read more >
Using React Refs in Typescript - Pluralsight
This guide will show how to use strongly typed refs using Typescript. We will see how to use refs from functional components, using...
Read more >
Virtualize large lists with react-window - web.dev
A render prop is used to return a function that the list component uses in order to render. Both onItemsRendered and ref attributes...
Read more >
react-window - Bountysource
Hello, is there a way to force react-window to re-render (refresh) the ... I did not go through every example) has react-window being...
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