From 9f3a15c47b7d31dd80c52dd1d210ea6be1799aea Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Thu, 14 Nov 2019 17:52:34 -0300 Subject: [PATCH] fix(react): fix passing ref to wrapper not working properly (fix #409) --- packages/simplebar-react/index.js | 8 ++++---- packages/simplebar/demo/Demo.js | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/simplebar-react/index.js b/packages/simplebar-react/index.js index f42eb728..7fec060b 100644 --- a/packages/simplebar-react/index.js +++ b/packages/simplebar-react/index.js @@ -37,8 +37,8 @@ const getOptions = function(obj) { const SimpleBar = React.forwardRef( ({ children, scrollableNodeProps = {}, ...otherProps }, ref) => { let instance; - const elRef = useRef({}); - const scrollableNodeRef = useRef(); + let scrollableNodeRef = useRef(); + const elRef = useRef(); const contentNodeRef = useRef(); let options = {}; let rest = {}; @@ -66,7 +66,7 @@ const SimpleBar = React.forwardRef( } useEffect(() => { - const scrollableNodeRef = scrollableNodeProps.ref || scrollableNodeRef; + scrollableNodeRef = scrollableNodeProps.ref || scrollableNodeRef; if (elRef.current) { instance = new SimpleBarJS(elRef.current, { @@ -89,7 +89,7 @@ const SimpleBar = React.forwardRef( instance.unMount(); instance = null; }; - }); + }, []); return (
diff --git a/packages/simplebar/demo/Demo.js b/packages/simplebar/demo/Demo.js index bc8fa1c4..4ecb3924 100755 --- a/packages/simplebar/demo/Demo.js +++ b/packages/simplebar/demo/Demo.js @@ -24,7 +24,7 @@ const renderScrollbar = props => { const Demo = () => { const [isHidden, setHidden] = React.useState(true); - const scrollableElRef = React.createRef(); + // const scrollableElRef = React.createRef(); const handleShowClick = React.useCallback(() => { setHidden(false); @@ -282,7 +282,7 @@ const Demo = () => { className="demo1" autoHide={false} data-simplebar-force-visible="x" - scrollableNodeProps={{ ref: scrollableElRef }} + // scrollableNodeProps={{ ref: scrollableElRef }} > {({ scrollableNodeRef, contentNodeRef }) => ( {
+
+
+

SimpleBar-React + refs

+ + {({ scrollableNodeRef, contentNodeRef }) => { + return ( +
+
+ {[...Array(50)].map((x, i) => ( +

Some content

+ ))} +
+
+ ); + }} +
+
+
); };