File tree Expand file tree Collapse file tree 5 files changed +50
-24
lines changed Expand file tree Collapse file tree 5 files changed +50
-24
lines changed Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
3
- export default class Counter extends React . Component {
4
- constructor ( props ) {
5
- super ( props )
6
- this . state = { counter : 0 }
3
+ export function withIncrement ( Component ) {
4
+ return class extends React . Component {
5
+ constructor ( props ) {
6
+ super ( props )
7
+ this . state = { counter : 0 }
8
+ this . incrementCounter = this . incrementCounter . bind ( this )
9
+ }
10
+
11
+ incrementCounter ( ) {
12
+ this . setState ( state => ( { counter : state . counter + 1 } ) )
13
+ }
14
+
15
+ render ( ) {
16
+ return (
17
+ < Component counter = { this . state . counter } increment = { this . incrementCounter } />
18
+ )
19
+ }
7
20
}
21
+ }
8
22
23
+ export default class Counter extends React . Component {
9
24
componentDidMount ( ) {
10
- document . title = `Class ${ this . state . counter } `
25
+ document . title = `Class ${ this . props . counter } `
11
26
12
- this . interval = setInterval ( ( ) => {
13
- this . setState ( state => ( { counter : state . counter + 1 } ) )
14
- } , 2000 )
27
+ this . interval = setInterval ( this . props . increment , 2000 )
15
28
}
16
29
17
30
componentDidUpdate ( ) {
18
- document . title = `Class ${ this . state . counter } `
31
+ document . title = `Class ${ this . props . counter } `
19
32
}
20
33
21
34
componentWillUnmount ( ) {
@@ -26,8 +39,8 @@ export default class Counter extends React.Component {
26
39
return (
27
40
< div >
28
41
< h2 > Class component</ h2 >
29
- < p > { this . state . counter } </ p >
30
- < button onClick = { ( ) => this . setState ( state => ( { counter : state . counter + 1 } ) ) } > Increment class component</ button >
42
+ < p > { this . props . counter } </ p >
43
+ < button onClick = { this . props . increment } > Increment class component</ button >
31
44
</ div >
32
45
)
33
46
}
Original file line number Diff line number Diff line change 1
- import React , { useState , useEffect } from 'react'
1
+ import React , { useEffect } from 'react'
2
+ import { useIncremental } from './useIncremental'
3
+ import { useTitle } from './useTitle'
2
4
3
- export default function ( props ) {
4
- const [ counter , setCounter ] = useState ( 0 )
5
+ export default function ( ) {
6
+ const [ counter , incrementCounter ] = useIncremental ( 0 )
7
+ useTitle ( `Functional ${ counter } ` )
5
8
6
9
useEffect ( ( ) => {
7
- document . title = `Functional ${ counter } `
8
- } )
9
-
10
- useEffect ( ( ) => {
11
- const interval = setInterval ( ( ) => {
12
- setCounter ( counter => counter + 1 )
13
- } , 2500 )
10
+ const interval = setInterval ( incrementCounter , 2500 )
14
11
15
12
return ( ) => clearInterval ( interval )
16
13
} )
@@ -19,7 +16,7 @@ export default function (props) {
19
16
< div >
20
17
< h2 > Functional component</ h2 >
21
18
< p > { counter } </ p >
22
- < button onClick = { ( ) => setCounter ( counter => counter + 1 ) } > Increment Functional</ button >
19
+ < button onClick = { incrementCounter } > Increment Functional</ button >
23
20
</ div >
24
21
)
25
22
}
Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
import ReactDOM from 'react-dom'
3
- import ClassComponent from './ClassComponent'
3
+ import ClassComponent , { withIncrement } from './ClassComponent'
4
4
import FunctionalComponent from './FunctionalComponent'
5
5
6
+ const IncrementalComponent = withIncrement ( ClassComponent )
7
+
6
8
function App ( ) {
7
9
return (
8
10
< >
9
- < ClassComponent />
11
+ < IncrementalComponent />
10
12
< FunctionalComponent />
11
13
</ >
12
14
)
Original file line number Diff line number Diff line change
1
+ import { useState } from 'react'
2
+
3
+ export function useIncremental ( initialValue ) {
4
+ const [ counter , setCounter ] = useState ( initialValue )
5
+
6
+ return [ counter , ( ) => setCounter ( counter => counter + 1 ) ]
7
+ }
Original file line number Diff line number Diff line change
1
+ import { useEffect } from 'react'
2
+
3
+ export function useTitle ( title ) {
4
+ useEffect ( ( ) => {
5
+ document . title = title
6
+ } , [ title ] )
7
+ }
You can’t perform that action at this time.
0 commit comments