Skip to content

Commit 4a02dc5

Browse files
authored
Refactor class components to functional @ examples (vercel#13398)
**Affected examples** with-segment-analytics with-slate with-portals-ssr ( _with-portals-ssr is based on a package not being updated in the last year_) with-videojs with-next-page-transitions with-firebase-cloud-messaging with-dynamic-app-layout with-dynamic-import with-next-transition with-carbon-components with-cerebral with-custom-babel-config Here and there I have removed some redundant imports as well. I believe with this PR, there are only 1 or 2 examples left using class-based components. If by any chance you find any, let me know and I'll refactor them too. If you don't like anything or you want me to change something, please let me know. **If there is anything else you'd like me to help with, I would be honored to assist.**
1 parent dcf446d commit 4a02dc5

File tree

15 files changed

+247
-301
lines changed

15 files changed

+247
-301
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Component, Fragment } from 'react'
21
import { Button } from 'carbon-components-react'
32

4-
export default class DemoApp extends Component {
5-
render() {
6-
return (
7-
<Fragment>
8-
<Button>Hello, world!</Button>
9-
</Fragment>
10-
)
11-
}
3+
const Home = () => {
4+
return (
5+
<>
6+
<Button>Hello, world!</Button>
7+
</>
8+
)
129
}
10+
11+
export default Home

examples/with-cerebral/pages/index.js

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import { Component } from 'react'
21
import { Controller } from 'cerebral'
32
import Devtools from 'cerebral/devtools'
43
import { Container } from '@cerebral/react'
54
import Page from '../components/Page'
65
import clock from '../modules/clock'
76

8-
export default class Counter extends Component {
9-
constructor(props) {
10-
super(props)
11-
// The controller will be instantiated for every page change and we only
12-
// add the devtools if we indeed are running in the browser
13-
this.controller = Controller({
14-
devtools:
15-
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
16-
? null
17-
: Devtools({ host: 'localhost:8787' }),
18-
modules: { clock },
19-
stateChanges: props.stateChanges,
20-
})
21-
}
22-
render() {
23-
return (
24-
<Container controller={this.controller}>
25-
<Page title="Index Page" linkTo="/other" />
26-
</Container>
27-
)
28-
}
7+
const Home = (props) => {
8+
const { stateChanges } = props
9+
10+
const controller = Controller({
11+
devtools:
12+
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
13+
? null
14+
: Devtools({ host: 'localhost:8787' }),
15+
modules: { clock },
16+
stateChanges: stateChanges,
17+
})
18+
19+
return (
20+
<Container controller={controller}>
21+
<Page title="Index Page" linkTo="/other" />
22+
</Container>
23+
)
2924
}
25+
26+
export default Home

examples/with-cerebral/pages/other.js

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import { Component } from 'react'
21
import { Controller } from 'cerebral'
32
import Devtools from 'cerebral/devtools'
43
import { Container } from '@cerebral/react'
54
import Page from '../components/Page'
65
import clock from '../modules/clock'
76

8-
export default class Counter extends Component {
9-
constructor(props) {
10-
super(props)
11-
// The controller will be instantiated for every page change and we only
12-
// add the devtools if we indeed are running in the browser
13-
this.controller = Controller({
14-
devtools:
15-
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
16-
? null
17-
: Devtools({ host: 'localhost:8787' }),
18-
modules: { clock },
19-
stateChanges: props.stateChanges,
20-
})
21-
}
22-
render() {
23-
return (
24-
<Container controller={this.controller}>
25-
<Page title="Other Page" linkTo="/" />
26-
</Container>
27-
)
28-
}
7+
const Other = (props) => {
8+
const { stateChanges } = props
9+
10+
const controller = Controller({
11+
devtools:
12+
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
13+
? null
14+
: Devtools({ host: 'localhost:8787' }),
15+
modules: { clock },
16+
stateChanges: stateChanges,
17+
})
18+
19+
return (
20+
<Container controller={controller}>
21+
<Page title="Index Page" linkTo="/other" />
22+
</Container>
23+
)
2924
}
25+
26+
export default Other
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
1-
import { Component } from 'react'
2-
export default class MyLuckNo extends Component {
3-
constructor(...args) {
4-
super(...args)
5-
this.state = { randomNo: null }
6-
}
1+
import { useState, useEffect } from 'react'
72

8-
componentDidMount() {
9-
this.recalculate()
10-
}
3+
const MyLuckNo = () => {
4+
const [randomNumber, setRandomNumber] = useState(null)
115

12-
recalculate() {
13-
this.setState({
14-
randomNo: Math.ceil(Math.random() * 100),
15-
})
6+
const recalculate = () => {
7+
setRandomNumber(Math.ceil(Math.random() * 100))
168
}
179

18-
render() {
19-
const { randomNo } = this.state
20-
21-
if (randomNo === null) {
22-
return <p>Please wait..</p>
23-
}
10+
useEffect(() => {
11+
recalculate()
12+
}, [])
2413

25-
// This is an experimental JavaScript feature where we can get with
26-
// using babel-preset-stage-0
27-
const message = do {
28-
if (randomNo < 30) {
29-
// eslint-disable-next-line no-unused-expressions
30-
;('Do not give up. Try again.')
31-
} else if (randomNo < 60) {
32-
// eslint-disable-next-line no-unused-expressions
33-
;('You are a lucky guy')
34-
} else {
35-
// eslint-disable-next-line no-unused-expressions
36-
;('You are soooo lucky!')
37-
}
14+
const message = do {
15+
if (randomNumber < 30) {
16+
// eslint-disable-next-line no-unused-expressions
17+
;('Do not give up. Try again.')
18+
} else if (randomNumber < 60) {
19+
// eslint-disable-next-line no-unused-expressions
20+
;('You are a lucky guy')
21+
} else {
22+
// eslint-disable-next-line no-unused-expressions
23+
;('You are soooo lucky!')
3824
}
39-
40-
return (
41-
<div>
42-
<h3>Your Lucky number is: "{randomNo}"</h3>
43-
<p>{message}</p>
44-
<button onClick={() => this.recalculate()}>Try Again</button>
45-
</div>
46-
)
4725
}
26+
27+
if (randomNumber === null) return <p>Please wait..</p>
28+
return (
29+
<div>
30+
<h3>Your Lucky number is: "{randomNumber}"</h3>
31+
<p>{message}</p>
32+
<button onClick={() => recalculate()}>Try Again</button>
33+
</div>
34+
)
4835
}
36+
37+
export default MyLuckNo

examples/with-dynamic-app-layout/pages/_app.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react'
21
import App from 'next/app'
32

43
const Noop = ({ children }) => children
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { Component } from 'react'
1+
import { useEffect } from 'react'
22
import { firebaseCloudMessaging } from '../utils/webPush'
33

4-
class Index extends Component {
5-
componentDidMount() {
4+
const Index = () => {
5+
useEffect(() => {
66
firebaseCloudMessaging.init()
7-
}
8-
render() {
9-
return <div>Next.js with firebase cloud messaging.</div>
10-
}
7+
}, [])
8+
9+
return <div>Next.js with firebase cloud messaging.</div>
1110
}
1211

1312
export default Index

examples/with-next-page-transitions/pages/about.js

+27-35
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
1-
import React from 'react'
1+
import { useState, useEffect } from 'react'
22
import PropTypes from 'prop-types'
33
import Link from 'next/link'
44

5-
class About extends React.Component {
6-
static pageTransitionDelayEnter = true
5+
const About = (props) => {
6+
const [loaded, setLoaded] = useState(false)
7+
const { pageTransitionReadyToEnter } = props
78

8-
constructor(props) {
9-
super(props)
10-
this.state = {
11-
loaded: false,
12-
}
13-
}
14-
15-
componentDidMount() {
16-
this.timeoutId = setTimeout(() => {
17-
this.props.pageTransitionReadyToEnter()
18-
this.setState({ loaded: true })
9+
useEffect(() => {
10+
const timeoutId = setTimeout(() => {
11+
pageTransitionReadyToEnter()
12+
setLoaded(true)
1913
}, 2000)
20-
}
21-
22-
componentWillUnmount() {
23-
if (this.timeoutId) clearTimeout(this.timeoutId)
24-
}
25-
26-
render() {
27-
if (!this.state.loaded) return null
28-
return (
29-
<div className="container bg-success page">
30-
<h1>About us</h1>
31-
<p>
32-
Notice how a loading spinner showed up while my content was "loading"?
33-
Pretty neat, huh?
34-
</p>
35-
<Link href="/">
36-
<a className="btn btn-light">Go back home</a>
37-
</Link>
38-
</div>
39-
)
40-
}
14+
return () => {
15+
clearTimeout(timeoutId)
16+
}
17+
}, [pageTransitionReadyToEnter])
18+
19+
if (!loaded) return null
20+
21+
return (
22+
<div className="container bg-success page">
23+
<h1>About us</h1>
24+
<p>
25+
Notice how a loading spinner showed up while my content was "loading"?
26+
Pretty neat, huh?
27+
</p>
28+
<Link href="/">
29+
<a className="btn btn-light">Go back home</a>
30+
</Link>
31+
</div>
32+
)
4133
}
4234

4335
About.propTypes = {

examples/with-portals-ssr/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dependencies": {
88
"@jesstelford/react-portal-universal": "1.0.0",
99
"next": "latest",
10-
"react": "^16.8.0",
11-
"react-dom": "^16.8.0"
10+
"react": "16.13.1",
11+
"react-dom": "16.13.1"
1212
}
1313
}

0 commit comments

Comments
 (0)