Skip to content

Commit

Permalink
Merge pull request #3867 from marmelab/fix-theme-change
Browse files Browse the repository at this point in the history
Fix theme can't be changed dynamically
  • Loading branch information
fzaninotto authored Oct 28, 2019
2 parents a81cbb9 + 4c2e555 commit 8590fdc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/ra-ui-materialui/src/layout/Layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { Component, createElement, useEffect, useRef } from 'react';
import React, {
Component,
createElement,
useEffect,
useRef,
useState,
} from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import classnames from 'classnames';
Expand Down Expand Up @@ -179,16 +185,18 @@ const EnhancedLayout = compose(
)(Layout);

const LayoutWithTheme = ({ theme: themeOverride, ...props }) => {
const theme = useRef(createMuiTheme(themeOverride));
const themeProp = useRef(themeOverride);
const [theme, setTheme] = useState(createMuiTheme(themeOverride));

useEffect(() => {
if (theme.current !== themeOverride) {
theme.current = createMuiTheme(themeOverride);
if (themeProp.current !== themeOverride) {
themeProp.current = themeOverride;
setTheme(createMuiTheme(themeOverride));
}
}, [themeOverride]);
}, [themeOverride, themeProp, theme, setTheme]);

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

0 comments on commit 8590fdc

Please sign in to comment.