Skip to content

Commit a751c58

Browse files
feat: Initial commit
0 parents  commit a751c58

File tree

12 files changed

+5581
-0
lines changed

12 files changed

+5581
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env
15+
.env.local
16+
.env.development.local
17+
.env.test.local
18+
.env.production.local
19+
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*
23+
.vscode/settings.json

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# NextJS with Redux and Material-UI example
2+
3+
A boilerplate NextJS with Redux and Material UI
4+
5+
## Getting Started
6+
7+
### Installing
8+
9+
Cloning the project.
10+
```
11+
git https://github.com/joaopaulomoraes/nextjs-with-redux-and-material-ui.git nextjs-with-redux-and-material-ui
12+
```
13+
14+
Access the project directory.
15+
```
16+
cd nextjs-with-redux-and-material-ui
17+
```
18+
19+
Install dependencies.
20+
```
21+
yarn install
22+
```
23+
24+
Serve with hot reload at http://localhost:3000.
25+
```
26+
yarn dev
27+
```
28+
29+
## The idea behind the example
30+
31+
In this example, we will display a counter that is initialized with a value of 0 and will be updated with each click. The first rendering is happening on the server, then the browser takes over. To illustrate this, the rendered counter will have a value of 1 when the app loads and a flag with the dispatch source will be displayed above the counter. From the next clicks that increment / decrement, the counter will receive its new value and the flag with the origin will be updated again with the origin of the dispatch.
32+
33+
![](https://i.imgur.com/6YQqLiL.gif)
34+
35+
Our page is located in `pages/index.js`, so it will map the `/` route. To get the initial data for rendering, we are implementing the `getInitialProps` static method, initializing the redux storage and dispatching the increment action, passing the isServer parameter to identify that the dispatch source is coming from the server. As the component is packaged with `next-redux-wrapper`, the component is automatically connected to Redux and packaged with the reagent-redux Provider`, which allows us to access the redux state immediately and send the storage to the child components for that they access the state when necessary.
36+
37+
For security, it is recommended to wrap all pages, whether they use Redux or not, so you do not worry about all the child components anymore.
38+
39+
The `withRedux` function accepts` makeStore` as the first argument, all other arguments are passed internally to the `react-redux connect ()` function. The `makeStore` function will receive the initialState as an argument and should return a new instance of redux store every time it is called, no memoisation is required here. See the [full example] (https://github.com/kirill-konshin/next-redux-wrapper#usage) in the Next Redux Wrapper repository. And there's another package [https://github.com/huzidaha/next-connect-redux] available with similar features.
40+
41+
To pass the initial state from the server to the client, we pass as a prop called `initialState`, so it is available when the client takes control.
42+
43+
The trick here to support the universal redux is to separate the cases for the client and the server. When we are on the server, we want to create a new store every time, otherwise the data from the different users will be mixed. If we are on the customer, we always want to use the same store. This is what we do in `store.js`
44+
45+
Again, the first render is happening in the server and instead of starting the count at 0, it will dispatch an action in redux that starts the count at 1. This continues to highlight how each navigation triggers a server render first and then a client render second, when you navigate between pages.

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "with-redux-wrapper-and-material-ui",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"@material-ui/core": "3.7.1",
6+
"@material-ui/icons": "3.0.2",
7+
"jss": "9.8.7",
8+
"next": "latest",
9+
"next-redux-wrapper": "latest",
10+
"react": "16.7.0",
11+
"react-dom": "16.7.0",
12+
"react-jss": "8.6.1",
13+
"react-redux": "6.0.0",
14+
"redux": "4.0.1",
15+
"redux-thunk": "2.3.0"
16+
},
17+
"scripts": {
18+
"dev": "next",
19+
"build": "next build",
20+
"start": "next start"
21+
},
22+
"devDependencies": {
23+
"redux-devtools-extension": "2.13.7"
24+
},
25+
"license": "ISC"
26+
}

pages/_app.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react'
2+
import App, { Container } from 'next/app'
3+
import Head from 'next/head'
4+
import withRedux from 'next-redux-wrapper'
5+
import { Provider } from 'react-redux'
6+
import { MuiThemeProvider } from '@material-ui/core/styles'
7+
import CssBaseline from '@material-ui/core/CssBaseline'
8+
import JssProvider from 'react-jss/lib/JssProvider'
9+
import store from '../src/store'
10+
import getPageContext from '../src/utils/getPageContext'
11+
12+
const _App = withRedux(store)(
13+
class _App extends App {
14+
pageContext = getPageContext()
15+
16+
static async getInitialProps ({ Component, ctx }) {
17+
return {
18+
pageProps: Component.getInitialProps
19+
? await Component.getInitialProps(ctx)
20+
: {}
21+
}
22+
}
23+
24+
componentDidMount () {
25+
const jssStyles = document.querySelector('#jss-server-side')
26+
if (jssStyles && jssStyles.parentNode) {
27+
jssStyles.parentNode.removeChild(jssStyles)
28+
}
29+
}
30+
31+
render () {
32+
const {
33+
Component,
34+
pageProps,
35+
store
36+
} = this.props
37+
38+
return (
39+
<Container>
40+
<Head>
41+
<title>NextJS - With Redux and Material UI</title>
42+
</Head>
43+
<JssProvider
44+
registry={this.pageContext.sheetsRegistry}
45+
generateClassName={this.pageContext.generateClassName}
46+
>
47+
<MuiThemeProvider
48+
theme={this.pageContext.theme}
49+
sheetsManager={this.pageContext.sheetsManager}
50+
>
51+
<CssBaseline />
52+
<Provider store={store}>
53+
<Component
54+
pageContext={this.pageContext}
55+
{...pageProps}
56+
/>
57+
</Provider>
58+
</MuiThemeProvider>
59+
</JssProvider>
60+
</Container>
61+
)
62+
}
63+
}
64+
)
65+
66+
export default _App

pages/_document.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React, { Fragment } from 'react'
2+
import PropTypes from 'prop-types'
3+
import Document, { Head, Main, NextScript } from 'next/document'
4+
import flush from 'styled-jsx/server'
5+
6+
class _Document extends Document {
7+
render () {
8+
const { pageContext } = this.props
9+
10+
return (
11+
<html lang='pt-BR' dir='ltr'>
12+
<Head>
13+
<meta charSet='utf-8' />
14+
<meta name='viewport' content='minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no' />
15+
<meta name='theme-color' content={pageContext ? pageContext.theme.palette.primary.main : null} />
16+
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto:300,400,500' />
17+
</Head>
18+
<body>
19+
<Main />
20+
<NextScript />
21+
</body>
22+
</html>
23+
)
24+
}
25+
}
26+
27+
_Document.getInitialProps = ctx => {
28+
let pageContext
29+
30+
const page = ctx.renderPage(Component => {
31+
const WrappedComponent = props => {
32+
pageContext = props.pageContext
33+
return <Component {...props} />
34+
}
35+
36+
WrappedComponent.propTypes = {
37+
pageContext: PropTypes.object.isRequired
38+
}
39+
40+
return WrappedComponent
41+
})
42+
43+
let css
44+
45+
if (pageContext) {
46+
css = pageContext.sheetsRegistry.toString()
47+
}
48+
49+
return {
50+
...page,
51+
pageContext,
52+
53+
styles: (
54+
<Fragment>
55+
<style
56+
id='jss-server-side'
57+
dangerouslySetInnerHTML={{ __html: css }}
58+
/>
59+
{flush() || null}
60+
</Fragment>
61+
)
62+
}
63+
}
64+
65+
export default _Document

pages/index.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React, { PureComponent } from 'react'
2+
import { withStyles } from '@material-ui/core/styles'
3+
import Card from '@material-ui/core/Card'
4+
import CardActions from '@material-ui/core/CardActions'
5+
import CardContent from '@material-ui/core/CardContent'
6+
import Fab from '@material-ui/core/Fab'
7+
import AddIcon from '@material-ui/icons/Add'
8+
import RemoveIcon from '@material-ui/icons/Remove'
9+
import Typography from '@material-ui/core/Typography'
10+
import { connect } from 'react-redux'
11+
import { increment, decrement } from '../src/actions'
12+
13+
const styles = theme => ({
14+
container: {
15+
display: 'flex',
16+
flexWrap: 'wrap'
17+
},
18+
title: {
19+
fontSize: 14
20+
}
21+
})
22+
23+
class Index extends PureComponent {
24+
static getInitialProps ({ store, isServer }) {
25+
store.dispatch(increment(isServer))
26+
27+
return { isServer }
28+
}
29+
30+
handleIncrement = () => {
31+
this.props.increment()
32+
}
33+
34+
handleDecrement = () => {
35+
this.props.decrement()
36+
}
37+
38+
render () {
39+
const { classes, counter } = this.props
40+
41+
return (
42+
<Card className={classes.card}>
43+
<CardContent>
44+
<Typography
45+
className={classes.title}
46+
color='textSecondary'
47+
gutterBottom
48+
>
49+
Dispatched from <b>{counter.from}</b>
50+
</Typography>
51+
<Typography variant='h3' component='h2'>
52+
{counter.value}
53+
</Typography>
54+
<Typography color='textSecondary'>{counter.action}</Typography>
55+
</CardContent>
56+
<CardActions>
57+
<Fab
58+
variant='round'
59+
color='primary'
60+
size='small'
61+
onClick={this.handleIncrement}
62+
>
63+
<AddIcon />
64+
</Fab>
65+
<Fab
66+
variant='round'
67+
color='secondary'
68+
size='small'
69+
onClick={this.handleDecrement}
70+
>
71+
<RemoveIcon />
72+
</Fab>
73+
</CardActions>
74+
</Card>
75+
)
76+
}
77+
}
78+
79+
const mapStateToProps = state => {
80+
return {
81+
counter: state
82+
}
83+
}
84+
85+
const mapDispatchToProps = dispatch => ({
86+
increment: () => dispatch(increment()),
87+
decrement: () => dispatch(decrement())
88+
})
89+
90+
export default connect(
91+
mapStateToProps,
92+
mapDispatchToProps
93+
)(withStyles(styles)(Index))

src/actions/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { INCREMENT, DECREMENT } from '../constants'
2+
3+
export const increment = (isServer) => {
4+
return dispatch => {
5+
dispatch({
6+
type: INCREMENT,
7+
from: isServer ? 'server' : 'client'
8+
})
9+
}
10+
}
11+
12+
export const decrement = (isServer) => {
13+
return dispatch => {
14+
dispatch({
15+
type: DECREMENT,
16+
from: isServer ? 'server' : 'client'
17+
})
18+
}
19+
}

src/constants/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const INCREMENT = 'INCREMENT'
2+
export const DECREMENT = 'DECREMENT'

src/reducers/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { INCREMENT, DECREMENT } from '../constants'
2+
3+
const initialState = {
4+
value: 0,
5+
action: null,
6+
from: null
7+
}
8+
9+
export const counter = (state = initialState, action) => {
10+
switch (action.type) {
11+
case INCREMENT:
12+
return {
13+
...state,
14+
value: state.value + 1,
15+
action: 'increment',
16+
from: action.from
17+
}
18+
19+
case DECREMENT:
20+
return {
21+
...state,
22+
value: state.value - 1,
23+
action: 'decrement',
24+
from: action.from
25+
}
26+
27+
default:
28+
return state
29+
}
30+
}

src/store/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createStore, applyMiddleware } from 'redux'
2+
import { composeWithDevTools } from 'redux-devtools-extension'
3+
import thunk from 'redux-thunk'
4+
import { counter } from '../reducers'
5+
6+
const store = initialState => {
7+
return createStore(
8+
counter,
9+
initialState,
10+
composeWithDevTools(applyMiddleware(thunk))
11+
)
12+
}
13+
14+
export default store

0 commit comments

Comments
 (0)