1. import { Button } from 'react-bootstrap';
    2. import React ,{useState, useLayoutEffect}from 'react'
    3. //使用useEffect,每次都会渲染这个0
    4. const Count = () => {
    5. const [count,setCount]=useState(10)
    6. useLayoutEffect(() => {
    7. if(count===0){
    8. setCount(Math.random())
    9. }
    10. }, [count])
    11. return (
    12. <div>
    13. <h2>数字:{count}</h2>
    14. <Button onClick={e=>setCount(0)}>修改数字</Button>
    15. </div>
    16. )
    17. }
    18. export default Count