Skip to content

Commit

Permalink
fix: Cannot read property 'scrollLeft' of null
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Jan 28, 2021
1 parent 738f076 commit 83aca26
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/stickyScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,16 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr

React.useEffect(() => {
if (!scrollState.isHiddenScrollBar) {
setScrollState(state => ({
...state,
scrollLeft:
(scrollBodyRef.current.scrollLeft / scrollBodyRef.current?.scrollWidth) *
scrollBodyRef.current?.clientWidth,
}));
setScrollState(state => {
const bodyNode = scrollBodyRef.current;
if (!bodyNode) {
return state;
}
return {
...state,
scrollLeft: (bodyNode.scrollLeft / bodyNode.scrollWidth) * bodyNode.clientWidth,
};
});
}
}, [scrollState.isHiddenScrollBar]);

Expand Down

0 comments on commit 83aca26

Please sign in to comment.