Skip to content

Commit cd4a806

Browse files
committed
feat(Test): add memo tests
1 parent 6ea88c1 commit cd4a806

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/__test__/Test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: saber2pr
33
* @Date: 2019-12-06 17:41:19
44
* @Last Modified by: saber2pr
5-
* @Last Modified time: 2019-12-11 21:02:47
5+
* @Last Modified time: 2019-12-13 16:38:54
66
*/
77
import React, { useState, useEffect, useRef } from ".."
88
import ReactDOM from "../client"
@@ -20,6 +20,7 @@ import { TestUseEffect } from "./TestUseEffect"
2020
import { TestForwardRef } from "./TestForwardRef"
2121
import { TestContext } from "./TestContext"
2222
import { TestLazy } from "./TestLazy"
23+
import { TestMemo } from "./TestMemo"
2324

2425
const Store = {
2526
state: 0
@@ -57,7 +58,8 @@ const Tests = [
5758
TestUseEffect,
5859
TestForwardRef,
5960
TestContext,
60-
TestLazy
61+
TestLazy,
62+
TestMemo
6163
]
6264

6365
const App = () => {

src/__test__/TestMemo.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: saber2pr
3+
* @Date: 2019-12-13 16:38:42
4+
* @Last Modified by: saber2pr
5+
* @Last Modified time: 2019-12-13 16:55:19
6+
*/
7+
import React, { useEffect, useState } from ".."
8+
9+
const Content = React.memo(({ content }: { content: string }) => {
10+
useEffect(() => {
11+
console.log("memo")
12+
})
13+
return <div>{content}</div>
14+
})
15+
16+
const useForceUpdate = () => {
17+
const [tick, setTick] = useState(0)
18+
return () => setTick(tick + 1)
19+
}
20+
21+
const TestMemo = () => {
22+
const [text, setText] = useState("awsl")
23+
const forceUpdate = useForceUpdate()
24+
return (
25+
<div>
26+
<div>TestMemo Demo</div>
27+
<div>
28+
<Content content={text} />
29+
</div>
30+
<div>
31+
<button onclick={() => setText("xmsl")}>setText1</button>
32+
<button onclick={() => setText("awsl")}>setText2</button>
33+
</div>
34+
<button onclick={forceUpdate}>forceUpdate</button>
35+
</div>
36+
)
37+
}
38+
39+
export { TestMemo }

0 commit comments

Comments
 (0)