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.

[react] add support for forwardRef

See original GitHub issue

🔎 Usage Questions

I am using the react warpper for simplebar and everything seems to work great but I am trying to get how much the user has scrolled and I tried the following:


export const ScrollableContent = ({ children }) => {
  const simplebar = useRef(null);

  useEffect(() => {
    if (simplebar.current) {
      simplebar.current
        .getScrollElement()
        .addEventListener('scroll', e => console.log(e));
    }
  }, [simplebar]);

  return (
    <>
      <ScrollBarCSS />
      <ScrollWrapper>
        <Content>
          <SimpleBar
            ref={simplebar}
          >
            {children}
          </SimpleBar>
        </Content>
      </ScrollWrapper>
    </>
  );
};

But it doesn’t seem to work and I don’t ever get a current in my ref

Does anyone have any ideas?

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
Grsmtocommented, Sep 21, 2019

Hey Sara,

Yeah actually we don’t use forwardRef so it prob doesn’t work as you would expect (actually you’re absolutely right in your code and that should definitely return the SimpleBar instance!). You can probably solve your issue using the scrollableNodeProps prop:

<SimpleBar
    scrollableNodeProps={{ ref: scrollableElRef }}
>
...

What you would get here is a ref to the DOM element directly (what getScrollElement() returns), so you won’t have to do getScrollElement. You can directly attach the listener to it in theory.

I realise that the React plugin (and actually Vue and Angular ones as well) don’t have any documentation yet. I’ll put the forwardRef and documentation as high priority.

Thanks for your report, hope this helps!

0reactions
Grsmtocommented, Oct 16, 2019

Hey @SaraVieira I ended up implementing this directly, thanks again for your help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forwarding Refs - React
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words,...
Read more >
How to use React's forwardRef function - Felix Gerschau
Our task is to forward the ref that the Input component receives to the HTML input element. We do that by wrapping the...
Read more >
Support for `forwardRef` components · Issue #187 - GitHub
I think the main alternative is to wrap this components inside an additional normal component that is reactive, extract the props that the ......
Read more >
How can I use forwardRef() in React? - Stack Overflow
The only way to pass a ref to a function component is using forwardRef. When using forwardRef, you can simply pass the ref...
Read more >
Forwarding Ref in React Hooks Env - Medium
The 'forwardRef' API supports to use Ref object in children components. If some function component is wrapped by this, the function component received...
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