Commit 58c2d138 authored by Olivier Tassinari's avatar Olivier Tassinari Committed by Tim Neutkens

examples(material-ui): change the wrapping strategy (#2648)

By creating an intermediary component (with the HOC)
we save some React rendering logic.
That's also addressing an issue with JssProvider (react-jss)
that generate a new index at each render.
parent 051ee314
import React, { Component } from 'react' import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { JssProvider } from 'react-jss' import { JssProvider } from 'react-jss'
import { withStyles, createStyleSheet, MuiThemeProvider } from 'material-ui/styles' import { withStyles, createStyleSheet, MuiThemeProvider } from 'material-ui/styles'
import { getContext } from '../styles/context' import { getContext } from '../styles/context'
// Apply some reset // Apply some reset
const styleSheet = createStyleSheet('App', theme => ({ const styleSheet = createStyleSheet(theme => ({
'@global': { '@global': {
html: { html: {
background: theme.palette.background.default, background: theme.palette.background.default,
...@@ -22,7 +21,16 @@ let AppWrapper = props => props.children ...@@ -22,7 +21,16 @@ let AppWrapper = props => props.children
AppWrapper = withStyles(styleSheet)(AppWrapper) AppWrapper = withStyles(styleSheet)(AppWrapper)
class App extends Component { function withRoot (BaseComponent) {
class WithRoot extends Component {
static getInitialProps (ctx) {
if (BaseComponent.getInitialProps) {
return BaseComponent.getInitialProps(ctx)
}
return {}
}
componentDidMount () { componentDidMount () {
// Remove the server-side injected CSS. // Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side') const jssStyles = document.querySelector('#jss-server-side')
...@@ -38,16 +46,17 @@ class App extends Component { ...@@ -38,16 +46,17 @@ class App extends Component {
<JssProvider registry={context.sheetsRegistry} jss={context.jss}> <JssProvider registry={context.sheetsRegistry} jss={context.jss}>
<MuiThemeProvider theme={context.theme} sheetsManager={context.sheetsManager}> <MuiThemeProvider theme={context.theme} sheetsManager={context.sheetsManager}>
<AppWrapper> <AppWrapper>
{this.props.children} <BaseComponent />
</AppWrapper> </AppWrapper>
</MuiThemeProvider> </MuiThemeProvider>
</JssProvider> </JssProvider>
) )
} }
} }
WithRoot.displayName = `withRoot(${BaseComponent.displayName})`
App.propTypes = { return WithRoot
children: PropTypes.node.isRequired
} }
export default App export default withRoot
...@@ -7,7 +7,7 @@ import Dialog, { ...@@ -7,7 +7,7 @@ import Dialog, {
DialogActions DialogActions
} from 'material-ui/Dialog' } from 'material-ui/Dialog'
import Typography from 'material-ui/Typography' import Typography from 'material-ui/Typography'
import App from '../components/App' import withRoot from '../components/withRoot'
const styles = { const styles = {
container: { container: {
...@@ -35,7 +35,6 @@ class Index extends Component { ...@@ -35,7 +35,6 @@ class Index extends Component {
render () { render () {
return ( return (
<App>
<div style={styles.container}> <div style={styles.container}>
<Dialog open={this.state.open} onRequestClose={this.handleRequestClose}> <Dialog open={this.state.open} onRequestClose={this.handleRequestClose}>
<DialogTitle>Super Secret Password</DialogTitle> <DialogTitle>Super Secret Password</DialogTitle>
...@@ -52,9 +51,8 @@ class Index extends Component { ...@@ -52,9 +51,8 @@ class Index extends Component {
Super Secret Password Super Secret Password
</Button> </Button>
</div> </div>
</App>
) )
} }
} }
export default Index export default withRoot(Index)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment