一般使用 setInterval(),setInterval() 方法會回傳 interval ID,我們可以透過 interval ID 將這個 setInterval() 清除 例如: ```js const intervalId = setInterval(() => { ... },1000) clearInterval(intervalId) ``` 當我們在 useEffect() 使用到 setInterval(),需要在元件移除時將 setInterval() 清除,可以在 useEffect() 裡面的 Effect 函式使用 return,並回傳另一個函式,這個函式會在該元件移除時被呼叫。 例如: ```js React.useEffect(() => { const intervalId = setInterval(() => { ... }, 1000) return () =>clearInterval(intervalId) },[]) ``` 這樣就可以透過 useEffect() 控制元件建立及移除時需要做的事。 ## 題目 請複製 (右下角 fork)這個[範例](https://codepen.io/yen-kg/pen/YzmMNZJ),在 Child 元件移除時執行 clear() 方法,完成後會呈現以下畫面。 ![](https://hackmd.io/_uploads/HkKlGEcs2.gif) ## 回報流程 將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/vftL5i0.png) <!-- 解答: ``` function Child({count, setCount}) { React.useEffect(() => { const intervalId = setInterval(() => { setCount(pre => pre + 1); }, 1000) return () =>clearInterval(intervalId) },[]) return <h1>Child 元件: {count}</h1> } ``` --> 回報區 --- | # | Discord | CodePen / 答案 | |:---:|:-------------- |:----------------------------------------------------------------------------- | | 1 | MY | [Codepen](https://codepen.io/ahmomoz/pen/OPLQEZX) | | 2 | Sonia | [Codepen](https://codepen.io/YUJOU/pen/YPKgqXd?editors=1011) | | 3 | LinaChen | [Codepen](https://codepen.io/LinaChen/pen/PwYrEMR) | | 4 | 毛巾 | [Codepen](https://codepen.io/bqdcjboa-the-solid/pen/QwLXQbY) | | 5 | han | [Codepen](https://codepen.io/iamHanCheng/pen/dPbBdGR)| | 6 | 4chan | [Codepen](https://codepen.io/ijuolaqc-the-looper/pen/KwPjQyP) | | 7 | tim | [Codepen](https://codepen.io/jskrtivy-the-animator/pen/MYgMQBK?editors=1010) | | 8 | Nocab | [Codepen](https://codepen.io/PeihanWang/pen/dPbBded?editors=1011) | | 9 | Yen | [Codepen](https://codepen.io/yuxxxlouyen/pen/emOwMLM) | | 10 | Noy(Toad) | [Codepen](https://codepen.io/MochiCodingPen/pen/NPKZMgX) | | 11 | 泊岸 | [Codepen](https://codepen.io/qoq77416416/pen/GgKbGjx?editors=1010) | |12|Hailey|[CodePen](https://codepen.io/sxbokfja-the-flexboxer/pen/XJrLqXo?editors=1011)| | 13 | Amanda | [Codepen](https://codepen.io/cym199922/pen/emOwjNW) | | 14 | 蘑菇星星 | [Codepen](https://codepen.io/brrrieon-the-vuer/pen/raBEryG) | | 15 | Clove | [Codepen](https://codesandbox.io/p/sandbox/p7vdfy) | | 16 | mercury2508. | [Codepen](https://codepen.io/Mercury2508/pen/LEPKaVm) | | 17 | Aaron 謝宗佑 | [Codepen](https://codepen.io/aaron-hsieh/pen/WbeqmZx) | | 18 | 嚼勁先生 | [Codepen](https://codepen.io/James520284/pen/KwPjYzQ) | | 19 | ya_meow | [Codepen](https://codepen.io/gkfxzvcb-the-bashful/pen/raBXxjq?editors=0010)| | 20 | Johnson | [Codepen](https://codepen.io/crpbugqy-the-typescripter/pen/GgRKpQe)| | 21 | Kaya | [Codepen](https://codepen.io/kayaribi/pen/raNBqrB)| | 22 | Jasmine | [Codepen](https://codepen.io/Jasmine-Lin-the-vuer/pen/XJWWOJx) | | 23 |邵|[Codepen](https://codepen.io/ukscrlno-the-typescripter/pen/dPyPOvy?editors=1010)| | 24 |Rogan|[Codepen](https://codepen.io/RoganHsu/pen/emYmyXy?editors=1011)| | 25 | Toung | [Codepen](https://codepen.io/Toung/pen/jEOEoBG) | | 26 | yaoling.liang | [Codepen](https://codepen.io/Yao-Ling-L-/pen/OPJNeab?editors=1011) | | 27 | 郭芙蘭 | [Codepen](https://codepen.io/flora_Kuo/pen/NPWRVVm?editors=0010) | | 28 | KOMATSU PEI | [Codepen](https://codepen.io/Komatsu2021/pen/GgRQpGa?editors=1010) | | 29 | 姜承 | [Codepen](https://codepen.io/Troy0718/pen/xbxajNr) | | 30 | jinliu214 | [Codepen](https://codepen.io/jinliu214/pen/PwwLjjP?editors=0010) | | 31 | chris | [Codepen](https://codepen.io/chris-chen-the-selector/pen/RNPddmj) | | 32 | shin_kai | [Codepen](https://codepen.io/KAI-SHIN-the-animator/pen/LEGxRVG) | --- - 快速複製格式: ```markdown! | 0 | user | [Codepen]() | ```