Ref not updating when conditionally applied
See original GitHub issueBug Report
Describe the Bug
A clear and concise description of what the bug is.
How to Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Conditionally apply the
ref
- Scroll to bottom
- Observe no callbacks fired and no entry exists
CodeSandbox Link
https://codesandbox.io/s/react-cool-inviewissues493-yks98
Expected Behavior
I would expect the observer to fire callbacks
Screenshots
Your Environment
- Device: MacBook Pro
- OS: macOS X
- Browser: Chrome
- Version: 1.2.2
Additional Information
Seems to be an issue when ref
is conditionally set.
{hasNextPage && <div ref={triggerRef}>Loading...</div>}
{items.map((item, i) => {
return i === items.length - 1 ? (
<div ref={lastItemRef} key={item.id}>
{item.id}
</div>
) : (
<div key={item.id}>{item.id}</div>
);
})}
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
React useRef not updating consistently for conditionally ...
In React when a ref changes, it doesn't trigger a component update, and useEffect s are not triggered. I suggest to put your...
Read more >Pitfalls of Conditional Rendering and Refs in React
Since the Loading component doesn't have any ref set to it, React will not set the value of the ref. And since the...
Read more >Conditional Formatting not updating with new data
I use conditional formatting on a report that sometimes works as intended and sometimes does not. When I update the data (which is...
Read more >A Thoughtful Way To Use React's useRef() Hook
In a React component, useState and useReducer can cause your component to re-render each time there is a call to the update functions....
Read more >Referencing Values with Refs - React Docs
Unlike state, setting the ref's current value does not trigger a re-render. Don't read or write ref.current during rendering. This makes your component...
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
@stramel Thank you for your support, I think this issue should be fixed in the latest version. BTW, I found you might forget to de-structure the created items from the demo code.
@wellyshen No worries! I appreciate your time on these bugs. It will be very beneficial as we start to use this in our projects at my company!