[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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top 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 >
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 Free
Top 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
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 thescrollableNodeProps
prop:What you would get here is a ref to the DOM element directly (what
getScrollElement()
returns), so you won’t have to dogetScrollElement
. 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!
Hey @SaraVieira I ended up implementing this directly, thanks again for your help.