Skip to content

Commit

Permalink
Merge pull request #3749 from marmelab/refactor-layout
Browse files Browse the repository at this point in the history
[RFR] Refactor Layout
  • Loading branch information
fzaninotto authored Sep 30, 2019
2 parents eb3d74d + ebb729e commit 6448d4b
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions packages/ra-ui-materialui/src/layout/Layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component, createElement } from 'react';
import React, { Component, createElement, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import classnames from 'classnames';
Expand Down Expand Up @@ -178,25 +178,21 @@ const EnhancedLayout = compose(
withStyles(styles)
)(Layout);

class LayoutWithTheme extends Component {
constructor(props) {
super(props);
this.theme = createMuiTheme(props.theme);
}
componentWillReceiveProps(nextProps) {
if (nextProps.theme !== this.props.theme) {
this.theme = createMuiTheme(nextProps.theme);
const LayoutWithTheme = ({ theme: themeOverride, ...props }) => {
const theme = useRef(createMuiTheme(themeOverride));

useEffect(() => {
if (theme.current !== themeOverride) {
theme.current = createMuiTheme(themeOverride);
}
}
render() {
const { theme, ...rest } = this.props;
return (
<ThemeProvider theme={this.theme}>
<EnhancedLayout {...rest} />
</ThemeProvider>
);
}
}
}, [themeOverride]);

return (
<ThemeProvider theme={theme.current}>
<EnhancedLayout {...props} />
</ThemeProvider>
);
};

LayoutWithTheme.propTypes = {
theme: PropTypes.object,
Expand Down

0 comments on commit 6448d4b

Please sign in to comment.