Giter Club home page Giter Club logo

Comments (6)

Sir814 avatar Sir814 commented on August 21, 2024 2

useEffect 没有加 deps 参数,组件每次渲染都会绑定一次点击事件,你加到5的试试,就会绑定5次,然后就会输出 5次

from blog.

panw3i avatar panw3i commented on August 21, 2024 1

很好的文章 可以补充一下 useCallback ref

from blog.

mode365Dong avatar mode365Dong commented on August 21, 2024 1

好全面啊,star

from blog.

beichensky avatar beichensky commented on August 21, 2024

@panw3i 好的👌🏻

from blog.

hwx98 avatar hwx98 commented on August 21, 2024
useEffect(() => {
    const handleClick = function() {
        console.log('count: ', count);
    }
    window.addEventListener('click', handleClick, false)
});

return <p onClick={() => setCount(count + 1)}>clicked {count} times</p>;

为什么 count:5 的时候就就打印了 5次呢?

from blog.

beichensky avatar beichensky commented on August 21, 2024
useEffect(() => {
    const handleClick = function() {
        console.log('count: ', count);
    }
    window.addEventListener('click', handleClick, false)
});

return <p onClick={() => setCount(count + 1)}>clicked {count} times</p>;

为什么 count:5 的时候就就打印了 5次呢?

@hwx98 如楼上所说,useEffect 没有 deps 参数,那么在第一次渲染及后面的每次更新时,都会执行。所有每次走 useEffect 的时候,按照这里的逻辑,都会绑定一个 click 事件,也就意味着 count 到 5 的时候,绑定了5 次,所以再次点击,就会打印五次了。

from blog.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.