/
withRoot.js
47 lines (38 loc) · 1.19 KB
/
withRoot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react';
import PropTypes from 'prop-types';
import { MuiThemeProvider } from '@material-ui/core/styles';
import getPageContext from './getPageContext';
function withRoot(Component) {
class WithRoot extends React.Component {
UNSAFE_componentWillMount() {
this.pageContext = this.props.pageContext || getPageContext();
}
componentDidMount() {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles && jssStyles.parentNode) {
jssStyles.parentNode.removeChild(jssStyles);
}
}
pageContext = null;
render() {
// MuiThemeProvider makes the theme available down the React tree thanks to React context.
return (
<MuiThemeProvider theme={this.pageContext.theme} sheetsManager={this.pageContext.sheetsManager}>
<Component {...this.props} />
</MuiThemeProvider>
);
}
}
WithRoot.propTypes = {
pageContext: PropTypes.object,
};
WithRoot.getInitialProps = ctx => {
if (Component.getInitialProps) {
return Component.getInitialProps(ctx);
}
return {};
};
return WithRoot;
}
export default withRoot;