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.

Ref not updating when conditionally applied

See original GitHub issue

Bug 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:

  1. Conditionally apply the ref
  2. Scroll to bottom
  3. 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

image

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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wellyshencommented, Apr 3, 2021

@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.

async onEnter({ observe, unobserve }) {
    unobserve();

    await new Promise((res) => {
      // Simulating data coming in from API
      setTimeout(() => {
-       setItems([...items, createItems()]);
+       setItems([...items, ...createItems()]);
        res();
      }, 200);
    });
    observe();
  }
1reaction
stramelcommented, Apr 2, 2021

@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!

Read more comments on GitHub >

github_iconTop 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 >

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