Skip to content

Commit f50539f

Browse files
committed
wip after communaute-dev
1 parent 60c0992 commit f50539f

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/App.tsx

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1-
import { useState } from 'react'
1+
import {memo, useCallback, useEffect, useMemo, useState} from 'react'
22
import reactLogo from './assets/react.svg'
33
import viteLogo from '/vite.svg'
44
import './App.css'
55

6+
let appRenderingCount = 0
7+
8+
const MON_STYLE = { color: 'red'}
9+
610
function App() {
11+
appRenderingCount++
712
const [count, setCount] = useState(0)
13+
// const [count2, setCount2] = useState(0)
14+
const [date, setDate] = useState(new Date())
15+
16+
17+
18+
useEffect(() => {
19+
setInterval(() => { setDate(new Date())}, 2000)
20+
}, []);
21+
22+
console.log(`Rendering App #${appRenderingCount} ...`)
23+
24+
const result = useMemo(() => {
25+
console.log(`re-calculating result...`)
26+
return count + count2
27+
}, [count])
28+
//
29+
// const myCallback = useCallback(() => {
30+
//
31+
// }, [result])
832

933
return (
1034
<>
@@ -18,9 +42,13 @@ function App() {
1842
</div>
1943
<h1>Vite + React ({process.env.NODE_ENV})</h1>
2044
<div className="card">
21-
<button onClick={() => setCount((count) => count + 1)}>
22-
count is {count}
23-
</button>
45+
{42}<br/>
46+
{date.toISOString()}
47+
<MemoizedCounter style={MON_STYLE} name={1} count={count} onClick={() => setCount(count + 1)} />
48+
<MemoizedCounter style={MON_STYLE} name={2} count={count} onClick={() => setTimeout(() => {
49+
setCount(count + 1000);
50+
}, 2000)} />
51+
<MemoizedTodo />
2452
<p>
2553
Edit <code>src/App.tsx</code> and save to test HMR
2654
</p>
@@ -32,4 +60,24 @@ function App() {
3260
)
3361
}
3462

63+
function Counter(props: { name: number, style: Record<string, string>, count: number, onClick: () => void}) {
64+
console.log(`Rendering Counter #${props.name} during #${appRenderingCount}`)
65+
return (
66+
<button onClick={props.onClick} style={props.style}>
67+
count is {props.count}
68+
</button>
69+
)
70+
}
71+
72+
const MemoizedCounter = memo(Counter, (prev, next) => Object.is(prev.count, next.count))
73+
74+
75+
function Toto() {
76+
console.log(`Rendering Toto during #${appRenderingCount}`)
77+
return (<span onClick={}>Toto</span>)
78+
}
79+
80+
const MemoizedTodo = memo(Toto)
81+
82+
3583
export default App

0 commit comments

Comments
 (0)