1 useEffect(() => {
2 //your code goes here
3 return () => {
4 //your cleanup code codes here
5 };
6 },[]);
1function App() {
2 const [count, setCount] = useState(0);
3
4 useEffect(() => {
5 longResolve().then(() => {
6 alert(count);
7 });
8 }, []);
9
10 return (
11 <div>
12 <button
13 onClick={() => {
14 setCount(count + 1);
15 }}
16 >
17 Count: {count}
18 </button>
19 </div>
20 );
21}
1useEffect(() => {
2 // code goes here
3 return () => {
4 // cleanup code codes here
5 };
6 },[]);
1import React, { useState, useEffect } from 'react';
2function Example() {
3 const [count, setCount] = useState(0);
4
5 useEffect(() => { document.title = `You clicked ${count} times`; });
6 return (
7 <div>
8 <p>You clicked {count} times</p>
9 <button onClick={() => setCount(count + 1)}>
10 Click me
11 </button>
12 </div>
13 );
14}
1class Example extends React.Component {
2 constructor(props) {
3 super(props);
4 this.state = {
5 count: 0
6 };
7 }
8
9 componentDidMount() { document.title = `You clicked ${this.state.count} times`; } componentDidUpdate() { document.title = `You clicked ${this.state.count} times`; }
10 render() {
11 return (
12 <div>
13 <p>You clicked {this.state.count} times</p>
14 <button onClick={() => this.setState({ count: this.state.count + 1 })}>
15 Click me
16 </button>
17 </div>
18 );
19 }
20}