Skip to content

Commit

Permalink
use synchronous effect
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdrnf committed Mar 14, 2023
1 parent 8bc1cb5 commit 5ded5a2
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/mui-material/src/Tabs/ScrollbarSize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import debounce from '../utils/debounce';
import { ownerWindow } from '../utils';
import { ownerWindow, unstable_useEnhancedEffect as useEnhancedEffect } from '../utils';

const styles = {
width: 99,
Expand All @@ -21,22 +21,21 @@ export default function ScrollbarSize(props) {
const scrollbarHeight = React.useRef();
const nodeRef = React.useRef(null);

const setMeasurements = (element) => {
scrollbarHeight.current = element.offsetHeight - element.clientHeight;
const setMeasurements = () => {
scrollbarHeight.current = nodeRef.current.offsetHeight - nodeRef.current.clientHeight;
};

React.useEffect(() => {
const element = nodeRef.current;
useEnhancedEffect(() => {
const handleResize = debounce(() => {
const prevHeight = scrollbarHeight.current;
setMeasurements(element);
setMeasurements();

if (prevHeight !== scrollbarHeight.current) {
onChange(scrollbarHeight.current);
}
});

const containerWindow = ownerWindow(element);
const containerWindow = ownerWindow(nodeRef.current);
containerWindow.addEventListener('resize', handleResize);
return () => {
handleResize.clear();
Expand All @@ -45,7 +44,7 @@ export default function ScrollbarSize(props) {
}, [onChange]);

React.useEffect(() => {
setMeasurements(nodeRef.current);
setMeasurements();
onChange(scrollbarHeight.current);
}, [onChange]);

Expand Down

0 comments on commit 5ded5a2

Please sign in to comment.