Skip to content

Commit 56f6ee9

Browse files
treetipsusername
and
username
authored
Fix warnings and optimize codes (#27)
* refactor: update all dependencies * refactor: remove no need code * refactor: move css import from _document to _app * refactor: support Automatic Static Optimization * feat: support Custom /_error without /404 * refactor: add useCallback * refactor: remove initial state tree * refactor: change to strict=true * docs: add typescript-fsa and typescript-fsa-reducer * docs: remove yarn dependency * docs: add typescript version * refactor: change page actions to hooks * refactor: changing the page on the client side Co-authored-by: username <[email protected]>
1 parent 635c11c commit 56f6ee9

23 files changed

+2348
-3516
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ VSCode と prettier と TSLint によって、リアルタイムに整形と構
2828
- [MATERIAL-UI v4](https://material-ui.com/)
2929
- [Redux](https://redux.js.org/)
3030
- [redux-saga](https://redux-saga.js.org/)
31+
- [typescript-fsa](https://github.com/aikoven/typescript-fsa)
32+
- [typescript-fsa-reducer](https://github.com/dphilipson/typescript-fsa-reducers)
3133
- [TSLint](https://palantir.github.io/tslint/)
3234

3335
## Requirement
3436

3537
- [Google Chrome](https://www.google.com/intl/ja_ALL/chrome/)
3638
- [Visual Studio Code](https://code.visualstudio.com/)
37-
- [yarn](https://yarnpkg.com/lang/ja/)
39+
- TypeScript v3.7 or higher( [require Optional Chaining](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining) )
3840

3941
## Install Google Chrome addon
4042

Diff for: components/molecules/PageHeader.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Paper, Typography } from "@material-ui/core"
22
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
33
import React from "react"
4-
import { useSelector } from "react-redux"
5-
import { selectedPageSelector } from "../../store/page"
4+
import { usePage } from "../../hooks"
65

76
const useStyles = makeStyles((theme: Theme) =>
87
createStyles({
@@ -30,9 +29,10 @@ type Props = {}
3029
* Page header component
3130
* @param props Props
3231
*/
33-
export const PageHeader = function(props: Props) {
32+
export const PageHeader = function (props: Props) {
3433
const classes = useStyles(props)
35-
const selectedPage = useSelector(selectedPageSelector)
34+
const { selectedPage } = usePage()
35+
3636
return (
3737
<Paper square={true} className={classes.root}>
3838
<Typography variant="h1" color="inherit" className={classes.title}>

Diff for: components/organisms/ReduxSagaSample.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Props = {
3434
* redux-saga sample component
3535
* @param props {Props} props
3636
*/
37-
export const ReduxSagaSample = function(props: Props) {
37+
export const ReduxSagaSample = function (props: Props) {
3838
const {
3939
title,
4040
description,
@@ -49,7 +49,7 @@ export const ReduxSagaSample = function(props: Props) {
4949
const [previousResponseValue, setPreviousResponseValue] = useState<string>("")
5050

5151
useEffect(() => {
52-
if (!storeState.timestamp) {
52+
if (!storeState?.timestamp) {
5353
return
5454
}
5555
const fetchResult = `${storeState.timestamp} - ${storeState.input}`
@@ -62,7 +62,7 @@ export const ReduxSagaSample = function(props: Props) {
6262
}
6363
setResponseValues(responseValues)
6464
setPreviousResponseValue(fetchResult)
65-
}, [storeState.timestamp])
65+
}, [storeState?.timestamp])
6666

6767
const handleChangeInput = (e: React.ChangeEvent<HTMLInputElement>) => {
6868
const value = e.target.value || ""

Diff for: components/organisms/ResponsiveDrawer.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import IconButton from "@material-ui/core/IconButton"
33
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
44
import MenuIcon from "@material-ui/icons/Menu"
55
import React, { useState } from "react"
6-
import { useSelector } from "react-redux"
7-
import { selectedPageSelector } from "../../store/page"
6+
import { usePage } from "../../hooks"
87
import { Sidenavi } from "../organisms"
98

109
const drawerWidth = 290
@@ -55,10 +54,10 @@ type Props = {
5554
* Responsive drawer
5655
* @see https://material-ui.com/demos/drawers/#responsive-drawer
5756
*/
58-
export const ResponsiveDrawer = function(props: Props) {
57+
export const ResponsiveDrawer = function (props: Props) {
5958
const { children } = props
6059
const classes = useStyles(props)
61-
const selectedPage = useSelector(selectedPageSelector)
60+
const { selectedPage } = usePage()
6261
const [mobileOpen, setMobileOpen] = useState<boolean>(false)
6362
/**
6463
* Toggle drawer

Diff for: components/organisms/Sidenavi.tsx

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { List } from "@material-ui/core"
22
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
33
import SvgIcon from "@material-ui/core/SvgIcon"
4-
import { useDispatch, useSelector } from "react-redux"
54
import { Page, SiteInfo } from "../../constants"
6-
import { PageActions, selectedPageSelector } from "../../store/page"
5+
import { usePage } from "../../hooks"
76
import { NextListItem } from "../molecules"
87

98
const useStyles = makeStyles((theme: Theme) =>
@@ -50,13 +49,10 @@ type Props = {}
5049
* Side navigation component
5150
* @param props Props
5251
*/
53-
export const Sidenavi = function(props: Props) {
52+
export const Sidenavi = function (props: Props) {
5453
const classes = useStyles(props)
55-
const selectedPage = useSelector(selectedPageSelector)
56-
const dispatch = useDispatch()
57-
58-
const handleChangePage = (page: Page) => () =>
59-
dispatch(PageActions.changePage({ selectedPage: page }))
54+
const { selectedPage, changePage } = usePage()
55+
const handleChangePage = (page: Page) => () => changePage(page)
6056

6157
return (
6258
<div className={classes.root}>
@@ -65,7 +61,7 @@ export const Sidenavi = function(props: Props) {
6561
</div>
6662

6763
<List className={classes.list}>
68-
{Page.values.map(page => {
64+
{Page.values.map((page) => {
6965
const Icon = page.icon
7066
return (
7167
<NextListItem

Diff for: components/templates/Layout.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
22
import Head from "next/head"
33
import * as React from "react"
4-
import { useSelector } from "react-redux"
5-
import { selectedPageSelector } from "../../store/page"
4+
import { usePage } from "../../hooks"
65
import { ResponsiveDrawer } from "../organisms"
76

87
const useStyles = makeStyles((_: Theme) =>
@@ -18,10 +17,11 @@ type Props = {
1817
className?: string
1918
}
2019

21-
export const Layout = function(props: Props) {
20+
export const Layout = function (props: Props) {
2221
const { children, className } = props
2322
const classes = useStyles(props)
24-
const selectedPage = useSelector(selectedPageSelector)
23+
const { selectedPage } = usePage()
24+
2525
return (
2626
<section className={`${classes.root} ${className}`}>
2727
<Head>

Diff for: hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./useCounter"
2+
export * from "./usePage"
23
export * from "./useThinOut"

Diff for: hooks/useCounter.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useCallback } from "react"
12
import { useDispatch, useSelector } from "react-redux"
23
import { CounterActions, countSelector } from "../store/counter"
34

@@ -17,14 +18,21 @@ export const useCounter = (): Readonly<CounterOperators> => {
1718

1819
return {
1920
count: useSelector(countSelector),
20-
increment: () => dispatch(CounterActions.increment()),
21-
decrement: () => dispatch(CounterActions.decrement()),
22-
calculate: (inputNumber: number) => {
23-
dispatch(
24-
CounterActions.calculate({
25-
inputNumber: inputNumber,
26-
})
27-
)
28-
},
21+
increment: useCallback(() => dispatch(CounterActions.increment()), [
22+
dispatch,
23+
]),
24+
decrement: useCallback(() => dispatch(CounterActions.decrement()), [
25+
dispatch,
26+
]),
27+
calculate: useCallback(
28+
(inputNumber: number) => {
29+
dispatch(
30+
CounterActions.calculate({
31+
inputNumber: inputNumber,
32+
})
33+
)
34+
},
35+
[dispatch]
36+
),
2937
}
3038
}

Diff for: hooks/usePage.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useCallback } from "react"
2+
import { useDispatch, useSelector } from "react-redux"
3+
import { Page } from "../constants"
4+
import { PageActions, selectedPageSelector } from "../store/page"
5+
6+
type PageOperators = {
7+
selectedPage: Page
8+
changePage: (selectedPage: Page) => void
9+
}
10+
11+
/**
12+
* Page custom-hooks
13+
* @see https://reactjs.org/docs/hooks-custom.html
14+
*/
15+
export const usePage = (): Readonly<PageOperators> => {
16+
const dispatch = useDispatch()
17+
const pageState = useSelector(selectedPageSelector)
18+
19+
return {
20+
selectedPage: pageState,
21+
changePage: useCallback(
22+
(selectedPage: Page) => {
23+
dispatch(
24+
PageActions.changePage({
25+
selectedPage: selectedPage,
26+
})
27+
)
28+
},
29+
[dispatch]
30+
),
31+
}
32+
}

Diff for: hooks/useThinOut.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useCallback } from "react"
12
import { useDispatch, useSelector } from "react-redux"
23
import {
34
IReduxSagaState,
@@ -22,21 +23,27 @@ export const useThinOut = (): Readonly<ThinOutOperators> => {
2223
const reduxSagaDebounceState = useSelector(reduxSagaDebounceSelector)
2324
const reduxSagaThrottleState = useSelector(reduxSagaThrottleSelector)
2425

25-
const handleDebounce = (inputValue: string) => {
26-
dispatch(
27-
ReduxSagaActions.fetchDebounce({
28-
input: inputValue,
29-
})
30-
)
31-
}
26+
const handleDebounce = useCallback(
27+
(inputValue: string) => {
28+
dispatch(
29+
ReduxSagaActions.fetchDebounce({
30+
input: inputValue,
31+
})
32+
)
33+
},
34+
[dispatch]
35+
)
3236

33-
const handleThrottle = (inputValue: string) => {
34-
dispatch(
35-
ReduxSagaActions.fetchThrottle({
36-
input: inputValue,
37-
})
38-
)
39-
}
37+
const handleThrottle = useCallback(
38+
(inputValue: string) => {
39+
dispatch(
40+
ReduxSagaActions.fetchThrottle({
41+
input: inputValue,
42+
})
43+
)
44+
},
45+
[dispatch]
46+
)
4047

4148
return {
4249
debounce: handleDebounce,

Diff for: next.config.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const withCSS = require("@zeit/next-css")
21
const path = require("path")
32
const Dotenv = require("dotenv-webpack")
43

5-
module.exports = withCSS({
6-
webpack: config => {
4+
module.exports = {
5+
webpack: (config) => {
76
config.plugins = [
87
...config.plugins,
98
new Dotenv({
@@ -13,4 +12,4 @@ module.exports = withCSS({
1312
]
1413
return config
1514
},
16-
})
15+
}

0 commit comments

Comments
 (0)