-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Use _app.js instead of wrapping every page by withRoot() (#11989)
* [examples] Avoid wrapping every page by withRoot * ready to be merged
- Loading branch information
1 parent
09896d4
commit ca33d97
Showing
5 changed files
with
132 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import App, { Container } from 'next/app'; | ||
import { MuiThemeProvider } from '@material-ui/core/styles'; | ||
import CssBaseline from '@material-ui/core/CssBaseline'; | ||
import JssProvider from 'react-jss/lib/JssProvider'; | ||
import getPageContext from '../src/getPageContext'; | ||
|
||
class MyApp extends App { | ||
constructor(props) { | ||
super(props); | ||
this.pageContext = getPageContext(); | ||
} | ||
|
||
pageContext = null; | ||
|
||
componentDidMount() { | ||
// Remove the server-side injected CSS. | ||
const jssStyles = document.querySelector('#jss-server-side'); | ||
if (jssStyles && jssStyles.parentNode) { | ||
jssStyles.parentNode.removeChild(jssStyles); | ||
} | ||
} | ||
|
||
render() { | ||
const { Component, pageProps } = this.props; | ||
return ( | ||
<Container> | ||
{/* Wrap every page in Jss and Theme providers */} | ||
<JssProvider | ||
registry={this.pageContext.sheetsRegistry} | ||
generateClassName={this.pageContext.generateClassName} | ||
> | ||
{/* MuiThemeProvider makes the theme available down the React | ||
tree thanks to React context. */} | ||
<MuiThemeProvider | ||
theme={this.pageContext.theme} | ||
sheetsManager={this.pageContext.sheetsManager} | ||
> | ||
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */} | ||
<CssBaseline /> | ||
{/* Pass pageContext to the _document though the renderPage enhancer | ||
to render collected styles on server side. */} | ||
<Component pageContext={this.pageContext} {...pageProps} /> | ||
</MuiThemeProvider> | ||
</JssProvider> | ||
</Container> | ||
); | ||
} | ||
} | ||
|
||
export default MyApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable jsx-a11y/anchor-is-valid */ | ||
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Button from '@material-ui/core/Button'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import { withStyles } from '@material-ui/core/styles'; | ||
import Link from 'next/link'; | ||
|
||
const styles = theme => ({ | ||
root: { | ||
textAlign: 'center', | ||
paddingTop: theme.spacing.unit * 20, | ||
}, | ||
}); | ||
|
||
function About(props) { | ||
const { classes } = props; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Typography variant="display1" gutterBottom> | ||
Material-UI | ||
</Typography> | ||
<Typography variant="subheading" gutterBottom> | ||
about page | ||
</Typography> | ||
<Typography gutterBottom> | ||
<Link href="/"> | ||
<a>Go to the main page</a> | ||
</Link> | ||
</Typography> | ||
<Button variant="contained" color="primary"> | ||
Do nothing button | ||
</Button> | ||
</div> | ||
); | ||
} | ||
|
||
About.propTypes = { | ||
classes: PropTypes.object.isRequired, | ||
}; | ||
|
||
export default withStyles(styles)(About); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.