You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
587 B

2 years ago
  1. import { useEffect, useState } from 'react'
  2. function Test () {
  3. useEffect(() => {
  4. let timer = setInterval(() => {
  5. console.log('定时器执行了')
  6. }, 1000)
  7. return () => {
  8. // 清理定时器
  9. clearInterval(timer)
  10. }
  11. }, [])
  12. return (
  13. <div>this is test</div>
  14. )
  15. }
  16. function App () {
  17. const [flag, setFlag] = useState(true)
  18. return (
  19. <div>
  20. {flag ? <Test /> : null}
  21. <button onClick={() => setFlag(!flag)}>switch</button>
  22. </div>
  23. )
  24. }
  25. export default App