-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
862 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useState, useContext, createContext, memo } from 'react'; | ||
|
||
const ctx = createContext(0); | ||
|
||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
return ( | ||
<ctx.Provider value={num}> | ||
<div | ||
onClick={() => { | ||
update(1); | ||
}} | ||
> | ||
<Cpn /> | ||
</div> | ||
</ctx.Provider> | ||
); | ||
} | ||
|
||
const Cpn = memo(function () { | ||
console.log('Cpn render'); | ||
return ( | ||
<div> | ||
<Child /> | ||
</div> | ||
); | ||
}); | ||
|
||
function Child() { | ||
console.log('Child render'); | ||
const val = useContext(ctx); | ||
|
||
return <div>ctx: {val}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, memo, useCallback } from 'react'; | ||
|
||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
|
||
const addOne = useCallback(() => update((num) => num + 1), []); | ||
|
||
return ( | ||
<div> | ||
<Cpn onClick={addOne} /> | ||
{num} | ||
</div> | ||
); | ||
} | ||
|
||
const Cpn = memo(function ({ onClick }) { | ||
console.log('Cpn render'); | ||
return ( | ||
<div onClick={() => onClick()}> | ||
<Child /> | ||
</div> | ||
); | ||
}); | ||
|
||
function Child() { | ||
console.log('Child render'); | ||
return <p>i am child</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useState, useContext, createContext, memo } from 'react'; | ||
|
||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => update(num + 1)}>+ 1</button> | ||
<p>num is: {num}</p> | ||
<ExpensiveSubtree /> | ||
</div> | ||
); | ||
} | ||
|
||
function ExpensiveSubtree() { | ||
console.log('ExpensiveSubtree render'); | ||
return <p>i am child</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useState, useContext, createContext, memo } from 'react'; | ||
|
||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
|
||
return ( | ||
<div onClick={() => update(num + 100)}> | ||
<button | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
update(num + 1); | ||
}} | ||
> | ||
+ 1 | ||
</button> | ||
<p>num is: {num}</p> | ||
<ExpensiveSubtree /> | ||
</div> | ||
); | ||
} | ||
|
||
function ExpensiveSubtree() { | ||
console.log('ExpensiveSubtree render'); | ||
return <p>i am child</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useState } from 'react'; | ||
|
||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
return ( | ||
<div | ||
onClick={() => { | ||
update(1); | ||
}} | ||
> | ||
<Cpn /> | ||
</div> | ||
); | ||
} | ||
|
||
function Cpn() { | ||
console.log('cpn render'); | ||
return <div>cpn</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>性能优化demo</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="main.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useState } from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
|
||
// import App from './Simple'; | ||
import App from './Context'; | ||
// import App from './Hook'; | ||
// import App from './Principle_demo1'; | ||
// import App from './Principle_demo2'; | ||
// import App from './memo'; | ||
// import App from './useMemo'; | ||
|
||
ReactDOM.createRoot(document.getElementById('root')).render(<App />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useState, memo } from 'react'; | ||
|
||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
return ( | ||
<div onClick={() => update(num + 1)}> | ||
<Cpn num={num} name={'cpn1'} /> | ||
<Cpn num={0} name={'cpn2'} /> | ||
</div> | ||
); | ||
} | ||
|
||
const Cpn = memo(function ({ num, name }) { | ||
console.log('render ', name); | ||
return ( | ||
<div> | ||
{name}: {num} | ||
<Child /> | ||
</div> | ||
); | ||
}); | ||
|
||
function Child() { | ||
console.log('Child render'); | ||
return <p>i am child</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useState, useContext, createContext, useMemo } from 'react'; | ||
|
||
// 方式1:App提取 bailout四要素 | ||
// 方式2:ExpensiveSubtree用memo包裹 | ||
export default function App() { | ||
const [num, update] = useState(0); | ||
console.log('App render ', num); | ||
|
||
const Cpn = useMemo(() => <ExpensiveSubtree />, []); | ||
|
||
return ( | ||
<div onClick={() => update(num + 100)}> | ||
<p>num is: {num}</p> | ||
{Cpn} | ||
</div> | ||
); | ||
} | ||
|
||
function ExpensiveSubtree() { | ||
console.log('ExpensiveSubtree render'); | ||
return <p>i am child</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.