usage of ref with VariableSizeList does not re-render when ref.current is set
See original GitHub issueI’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:
- Created 5 years ago
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 whentest
is fail:The next time it’s called, the ref points at the newly rendered list. 😄
There’s a difference between how
renderChild
in your example androwRenderer
are being used. In your sandbox. you callrenderChild
directly, which will execute in place and return aChild
element. What’s returned fromParent
’s render ultimately looks like this:If you check where react-window uses the
children
prop, you’ll see it’s usingReact.createElement
and passing inrowRenderer
as the type. If we update your sanbox to useReact.createElement
instead of calling therenderChild
directly, what is returned fromParent
’s render would look (roughly) like this: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 forReact.createElement(Component, props)
, which ultimately becomes an object kinda like{ type: Component, props: props }
. NoticeComponent
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!