Skip to content
This repository was archived by the owner on Oct 8, 2023. It is now read-only.

Commit d5814e5

Browse files
refactor: Refactor component to functional stateless
- functions instead class - styles as hooks
1 parent ba34331 commit d5814e5

File tree

1 file changed

+50
-54
lines changed

1 file changed

+50
-54
lines changed

pages/index.js

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { PureComponent } from 'react'
2-
import { withStyles } from '@material-ui/core/styles'
1+
import React from 'react'
2+
import { makeStyles } from '@material-ui/styles'
33
import Card from '@material-ui/core/Card'
44
import CardActions from '@material-ui/core/CardActions'
55
import CardContent from '@material-ui/core/CardContent'
@@ -10,7 +10,7 @@ import Typography from '@material-ui/core/Typography'
1010
import { connect } from 'react-redux'
1111
import { increment, decrement } from '../src/actions'
1212

13-
const styles = theme => ({
13+
const useStyles = makeStyles({
1414
container: {
1515
display: 'flex',
1616
flexWrap: 'wrap'
@@ -20,60 +20,56 @@ const styles = theme => ({
2020
}
2121
})
2222

23-
class Index extends PureComponent {
24-
static getInitialProps ({ store, isServer }) {
25-
store.dispatch(increment(isServer))
23+
const Index = (props) => {
24+
const {
25+
counter,
26+
increment,
27+
decrement
28+
} = props
2629

27-
return { isServer }
28-
}
29-
30-
handleIncrement = () => {
31-
this.props.increment()
32-
}
30+
const classes = useStyles()
3331

34-
handleDecrement = () => {
35-
this.props.decrement()
36-
}
32+
return (
33+
<Card className={classes.card}>
34+
<CardContent>
35+
<Typography
36+
className={classes.title}
37+
color='textSecondary'
38+
gutterBottom
39+
>
40+
Dispatched from <b>{counter.from}</b>
41+
</Typography>
42+
<Typography variant='h3' component='h2'>
43+
{counter.value}
44+
</Typography>
45+
<Typography color='textSecondary'>{counter.action}</Typography>
46+
</CardContent>
47+
<CardActions>
48+
<Fab
49+
variant='round'
50+
color='primary'
51+
size='small'
52+
onClick={() => increment()}
53+
>
54+
<AddIcon />
55+
</Fab>
56+
<Fab
57+
variant='round'
58+
color='secondary'
59+
size='small'
60+
onClick={() => decrement()}
61+
>
62+
<RemoveIcon />
63+
</Fab>
64+
</CardActions>
65+
</Card>
66+
)
67+
}
3768

38-
render () {
39-
const { classes, counter } = this.props
69+
Index.getInitialProps = ({ store, isServer }) => {
70+
store.dispatch(increment(isServer))
4071

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-
}
72+
return { isServer }
7773
}
7874

7975
const mapStateToProps = state => {
@@ -90,4 +86,4 @@ const mapDispatchToProps = dispatch => ({
9086
export default connect(
9187
mapStateToProps,
9288
mapDispatchToProps
93-
)(withStyles(styles)(Index))
89+
)(Index)

0 commit comments

Comments
 (0)