Skip to content

Commit

Permalink
Fix theme can't be changed dynamically
Browse files Browse the repository at this point in the history
Closes #3863
  • Loading branch information
fzaninotto committed Oct 23, 2019
1 parent 0beac81 commit 4c2e555
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 4c2e555

Please sign in to comment.