File tree Expand file tree Collapse file tree 5 files changed +57
-64
lines changed
Expand file tree Collapse file tree 5 files changed +57
-64
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- import React from ' react' ;
2- import logo from './logo.svg' ;
3- import './App.css' ;
1+ import React from " react" ;
2+ import MyStatefulComponent from "./components/state" ;
3+ import MyStatelessComponent from "./components/hooks" ;
44
55function App ( ) {
66 return (
7- < div className = "App" >
8- < header className = "App-header" >
9- < img src = { logo } className = "App-logo" alt = "logo" />
10- < p >
11- Edit < code > src/App.js</ code > and save to reload.
12- </ p >
13- < a
14- className = "App-link"
15- href = "https://reactjs.org"
16- target = "_blank"
17- rel = "noopener noreferrer"
18- >
19- Learn React
20- </ a >
21- </ header >
22- </ div >
7+ < >
8+ < MyStatefulComponent />
9+ < MyStatelessComponent />
10+ </ >
2311 ) ;
2412}
2513
Original file line number Diff line number Diff line change 1+ import React , { useState , useEffect } from "react" ;
2+
3+ const MyStatelessComponent = ( ) => {
4+ const [ count , setCount ] = useState ( 0 ) ;
5+
6+ // Similar to componentDidMount and componentDidUpdate:
7+ useEffect ( ( ) => {
8+ // Update the document title using the browser API
9+ document . title = `You clicked ${ count } times` ;
10+ } ) ;
11+
12+ return (
13+ < div >
14+ < p > You clicked { count } times</ p >
15+ < button onClick = { ( ) => setCount ( count + 1 ) } > Click me</ button >
16+ </ div >
17+ ) ;
18+ } ;
19+
20+ export default MyStatelessComponent ;
Original file line number Diff line number Diff line change 1+ import React , { Component } from "react" ;
2+
3+ class MyStatefulComponent extends Component {
4+ constructor ( props ) {
5+ super ( props ) ;
6+ this . state = {
7+ count : 0 ,
8+ } ;
9+ }
10+
11+ componentDidMount ( ) {
12+ document . title = `You clicked ${ this . state . count } times` ;
13+ }
14+ componentDidUpdate ( ) {
15+ document . title = `You clicked ${ this . state . count } times` ;
16+ }
17+
18+ render ( ) {
19+ return (
20+ < div >
21+ < p > You clicked { this . state . count } times</ p >
22+ < button onClick = { ( ) => this . setState ( { count : this . state . count + 1 } ) } >
23+ Click me
24+ </ button >
25+ </ div >
26+ ) ;
27+ }
28+ }
29+
30+ export default MyStatefulComponent ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments