-
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
23 changed files
with
884 additions
and
60 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,40 @@ | ||
import { useState, use, useEffect } from 'react'; | ||
|
||
const delay = (t) => | ||
new Promise((r) => { | ||
setTimeout(r, t); | ||
}); | ||
|
||
const cachePool: any[] = []; | ||
|
||
function fetchData(id, timeout) { | ||
const cache = cachePool[id]; | ||
if (cache) { | ||
return cache; | ||
} | ||
return (cachePool[id] = delay(timeout).then(() => { | ||
return { data: Math.random().toFixed(2) * 100 }; | ||
})); | ||
} | ||
|
||
export function Cpn({ id, timeout }) { | ||
const [num, updateNum] = useState(0); | ||
const { data } = use(fetchData(id, timeout)); | ||
|
||
if (num !== 0 && num % 5 === 0) { | ||
cachePool[id] = null; | ||
} | ||
|
||
useEffect(() => { | ||
console.log('effect create'); | ||
return () => console.log('effect destroy'); | ||
}, []); | ||
|
||
return ( | ||
<ul onClick={() => updateNum(num + 1)}> | ||
<li>ID: {id}</li> | ||
<li>随机数: {data}</li> | ||
<li>状态: {num}</li> | ||
</ul> | ||
); | ||
} |
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>Suspense</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,40 @@ | ||
import { Fragment, Suspense, useState } from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { Cpn } from './Cpn'; | ||
|
||
// 简单例子 + 没有Suspense catch的情况 | ||
function App() { | ||
return ( | ||
<Suspense fallback={<div>loading...</div>}> | ||
<Cpn id={0} timeout={1000} /> | ||
</Suspense> | ||
// <Cpn id={0} timeout={1000} /> | ||
); | ||
} | ||
|
||
// 嵌套Suspense | ||
// function App() { | ||
// return ( | ||
// <Suspense fallback={<div>外层...</div>}> | ||
// <Cpn id={0} timeout={1000} /> | ||
// <Suspense fallback={<div>内层...</div>}> | ||
// <Cpn id={1} timeout={3000} /> | ||
// </Suspense> | ||
// </Suspense> | ||
// ); | ||
// } | ||
|
||
// 缓存快速失效 | ||
// function App() { | ||
// const [num, setNum] = useState(0); | ||
// return ( | ||
// <div> | ||
// <button onClick={() => setNum(num + 1)}>change id: {num}</button> | ||
// <Suspense fallback={<div>loading...</div>}> | ||
// <Cpn id={num} timeout={2000} /> | ||
// </Suspense> | ||
// </div> | ||
// ); | ||
// } | ||
|
||
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 @@ | ||
/// <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
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
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.