Skip to content

Commit 02302b2

Browse files
authored
Optimize return type and add destructuring assignment. (#16)
* refactor: update dependencies * refactor: add return type and add destructuring assignment. * feat: add sample to pass parameter from server to client
1 parent e1d019a commit 02302b2

File tree

6 files changed

+1800
-2170
lines changed

6 files changed

+1800
-2170
lines changed

package-lock.json

Lines changed: 1736 additions & 2145 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@
1414
"author": "",
1515
"license": "ISC",
1616
"dependencies": {
17-
"@material-ui/core": "^4.5.1",
17+
"@material-ui/core": "^4.8.0",
1818
"@material-ui/icons": "^4.5.1",
19-
"@material-ui/styles": "^4.5.0",
19+
"@material-ui/styles": "^4.7.1",
2020
"@zeit/next-css": "^1.0.1",
2121
"compression": "^1.7.4",
2222
"express": "^4.17.1",
2323
"immer": "^4.0.2",
24-
"next": "^9.1.1",
24+
"next": "^9.1.5",
2525
"next-redux-wrapper": "^4.0.1",
26-
"react": "^16.11.0",
27-
"react-dom": "^16.11.0",
28-
"react-redux": "^7.1.1",
26+
"react": "^16.12.0",
27+
"react-dom": "^16.12.0",
28+
"react-redux": "^7.1.3",
2929
"redux": "^4.0.4",
3030
"typescript-fsa": "^3.0.0",
3131
"typescript-fsa-reducers": "^1.2.1"
3232
},
3333
"devDependencies": {
34-
"@babel/plugin-proposal-decorators": "^7.6.0",
34+
"@babel/plugin-proposal-decorators": "^7.7.4",
3535
"@types/next-redux-wrapper": "^2.0.2",
36-
"@types/react": "^16.9.11",
37-
"@types/react-dom": "^16.9.3",
36+
"@types/react": "^16.9.16",
37+
"@types/react-dom": "^16.9.4",
3838
"@types/react-jss": "^8.6.4",
3939
"@types/react-redux": "^7.1.5",
4040
"@types/redux": "^3.6.0",
4141
"@types/styled-jsx": "^2.2.8",
42-
"prettier": "^1.18.2",
42+
"prettier": "^1.19.1",
4343
"redux-devtools-extension": "^2.13.8",
44-
"tslint": "^5.20.0",
44+
"tslint": "^5.20.1",
4545
"tslint-config-prettier": "^1.18.0",
4646
"tslint-config-standard": "^8.0.1",
4747
"tslint-plugin-prettier": "^2.0.1",
48-
"typescript": "^3.6.4"
48+
"typescript": "^3.7.3"
4949
}
5050
}

pages/_error.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ function Error(props: Props) {
3838
* Server side rendering
3939
*/
4040
Error.getInitialProps = async (ctx: AppContext): Promise<Props> => {
41+
const { res, store } = ctx
42+
4143
const pagePayload: IPagePayload = {
4244
selectedPage: Page.ERROR,
4345
}
44-
ctx.store.dispatch({
46+
store.dispatch({
4547
type: PageActions.changePage.toString(),
4648
payload: pagePayload,
4749
})
4850
return {
49-
httpStatusCode: ctx.res.statusCode,
51+
httpStatusCode: res.statusCode,
5052
}
5153
}
5254

pages/about.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ const useStyles = makeStyles((_: Theme) =>
1414
})
1515
)
1616

17-
function About() {
18-
const classes = useStyles({})
17+
type Props = {}
18+
19+
function About(props: Props) {
20+
const classes = useStyles(props)
1921
return (
2022
<Layout className={classes.root}>
2123
<HeaderArticleContainer>
@@ -30,14 +32,17 @@ function About() {
3032
/**
3133
* Server side rendering
3234
*/
33-
About.getInitialProps = async (ctx: AppContext) => {
35+
About.getInitialProps = async (ctx: AppContext): Promise<Props> => {
36+
const { store } = ctx
37+
3438
const pagePayload: IPagePayload = {
3539
selectedPage: Page.ABOUT,
3640
}
37-
ctx.store.dispatch({
41+
store.dispatch({
3842
type: PageActions.changePage.toString(),
3943
payload: pagePayload,
4044
})
45+
return {}
4146
}
4247

4348
export default About

pages/index.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { Typography } from "@material-ui/core"
22
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
3+
import { AppContext } from "../components/AppContext"
34
import { SpacingPaper } from "../components/atoms"
45
import { HeaderArticleContainer } from "../components/organisms"
56
import { Layout } from "../components/templates"
7+
import { Page } from "../constants"
8+
import { IPagePayload, PageActions } from "../store/page"
69

710
const useStyles = makeStyles((_: Theme) =>
811
createStyles({
912
root: {},
1013
})
1114
)
1215

13-
function Index() {
14-
const classes = useStyles({})
16+
type Props = {}
17+
18+
function Index(props: Props) {
19+
const classes = useStyles(props)
1520
return (
1621
<Layout className={classes.root}>
1722
<HeaderArticleContainer>
@@ -30,4 +35,20 @@ function Index() {
3035
)
3136
}
3237

38+
/**
39+
* Server side rendering
40+
*/
41+
Index.getInitialProps = async (ctx: AppContext): Promise<Props> => {
42+
const { store } = ctx
43+
44+
const pagePayload: IPagePayload = {
45+
selectedPage: Page.TOP,
46+
}
47+
store.dispatch({
48+
type: PageActions.changePage.toString(),
49+
payload: pagePayload,
50+
})
51+
return {}
52+
}
53+
3354
export default Index

pages/redux.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ const useStyles = makeStyles((theme: Theme) =>
3030
})
3131
)
3232

33-
function Redux() {
34-
const classes = useStyles({})
33+
type Props = {
34+
// passed from getInitialProps
35+
defaultInputNumber: number
36+
}
37+
38+
function Redux(props: Props) {
39+
const { defaultInputNumber: defaultCount } = props
40+
const classes = useStyles(props)
3541
const dispatch = useDispatch()
3642
const count = useSelector(countSelector)
37-
const [inputNumber, setInputNumber] = useState<number>(10)
43+
const [inputNumber, setInputNumber] = useState<number>(defaultCount)
3844

3945
/**
4046
* Increment
@@ -122,14 +128,19 @@ function Redux() {
122128
/**
123129
* Server side rendering
124130
*/
125-
Redux.getInitialProps = async (ctx: AppContext) => {
131+
Redux.getInitialProps = async (ctx: AppContext): Promise<Props> => {
132+
const { store } = ctx
133+
126134
const pagePayload: IPagePayload = {
127135
selectedPage: Page.REDUX,
128136
}
129-
ctx.store.dispatch({
137+
store.dispatch({
130138
type: PageActions.changePage.toString(),
131139
payload: pagePayload,
132140
})
141+
return {
142+
defaultInputNumber: 2,
143+
}
133144
}
134145

135146
export default Redux

0 commit comments

Comments
 (0)