Skip to content

[ButtonBase] Derive state on render instead of in layout effects #26762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions packages/material-ui/src/ButtonBase/Ripple.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import useEventCallback from '../utils/useEventCallback';
import useEnhancedEffect from '../utils/useEnhancedEffect';

/**
* @ignore - internal component.
Expand All @@ -16,7 +14,7 @@ function Ripple(props) {
rippleY,
rippleSize,
in: inProp,
onExited = () => {},
onExited,
timeout,
} = props;
const [leaving, setLeaving] = React.useState(false);
Expand All @@ -37,21 +35,19 @@ function Ripple(props) {
[classes.childPulsate]: pulsate,
});

const handleExited = useEventCallback(onExited);
// Ripple is used for user feedback (e.g. click or press) so we want to apply styles with the highest priority
useEnhancedEffect(() => {
if (!inProp) {
// react-transition-group#onExit
setLeaving(true);

if (!inProp && !leaving) {
setLeaving(true);
}
React.useEffect(() => {
if (!inProp && onExited != null) {
// react-transition-group#onExited
const timeoutId = setTimeout(handleExited, timeout);
const timeoutId = setTimeout(onExited, timeout);
return () => {
clearTimeout(timeoutId);
};
}
return undefined;
}, [handleExited, inProp, timeout]);
}, [onExited, inProp, timeout]);

return (
<span className={rippleClassName} style={rippleStyles}>
Expand Down