简体   繁体   English

如何让 useEffect 只在组件挂载上运行一次而不对 React 撒谎?

[英]How to make useEffect only run once on component mount without lying to React?

If I want useEffect to only run once on component mount, what is the right way of doing it?如果我希望useEffect只在组件挂载上运行一次,那么正确的做法是什么? Check the following example:检查以下示例:

const UseEffectCounter1 = () => {
    const [count, setCount] = useState(0);
    useEffect(() => {
        document.title = `You clicked ${count} times`;
    }, []);
    return (
        <div>
            <button onClick={() => {setCount(count + 1)}}>+1</button>
        </div>
    )
}

Re-renders are prevented due to [] being passed as dependency array.由于[]作为依赖数组传递,因此阻止了重新渲染。 Code running fine, no errors.代码运行良好,没有错误。 'You clicked 0 times' shows only once, click the button does nothing, which fits the purpose 'only run once on the component mount'. 'You clicked 0 times'只显示一次,点击按钮什么都不做,符合'只在组件挂载上运行一次'的目的。

However this is lying to React and not recommended in Dan Abramov's article here .然而,这是对 React 的欺骗,并且在 Dan Abramov 的文章中不推荐。 I should pass the dependency to the dependency array, which is warned as React Hook useEffect has a missing dependency: 'count' .我应该将依赖项传递给依赖项数组,当React Hook useEffect has a missing dependency: 'count' However if I pass [count] in the dependency array, useEffect starts updating after component mount.但是,如果我在依赖数组中传递[count] ,则useEffect在组件挂载后开始更新。

What is the correct way of making useEffect only update on component mount?使useEffect仅更新组件安装的正确方法是什么?

As mentioned in the comment, there is no point in making the useEffect hook dependent on the count , if you are not going to update the title every time the count changes.如评论中所述,如果您不打算在每次计数更改时更新标题,则使useEffect挂钩依赖于count是没有意义的。 The code could be:代码可以是:

const UseEffectCounter1 = () => {
    const [count, setCount] = useState(0);
    useEffect(() => {
        document.title = 'You clicked 0 times';
    }, []);
    return (
        <div>
            <button onClick={() => {setCount(count + 1)}}>+1</button>
        </div>
    )
}

Anyway, if you really want to avoid the hard-coded value, you could do something like:无论如何,如果您真的想避免硬编码值,您可以执行以下操作:

const UseEffectCounter1 = () => {
    const initCount = 0;
    const [count, setCount] = useState(initCount);
    useEffect(() => {
        document.title = `You clicked ${initCount} times`;
    }, [initCount]);
    return (
        <div>
            <button onClick={() => {setCount(count + 1)}}>+1</button>
        </div>
    )
}

As initCount is never going to change, the useEffect hook will be triggered only once.由于initCount永远不会改变,useEffect 挂钩只会被触发一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复制每个组件安装的对象。 我如何使其只能运行一次? 在反应 - Objects duplicating every component mount. How can I make it run only once ? In react React.js / Leaflet — 仅在 DevServer 中运行 useEffect on Mount - React.js / Leaflet — Run useEffect on Mount only in DevServer 如何在组件安装以及 state 更改上运行 useEffect - How to run useEffect on component mount as well as state changes 如何使用 IntersectionObserver 使 React 组件在滚动时淡入淡出,但只有一次? - How to make a React component fade in on scroll using IntersectionObserver, but only once? 如何使它只运行一次? - How to make this run only once? 在我的情况下,如何只使用一次反应钩子 useEffect ? - How to use react hook useEffect only once in my case? 如何使用 React useEffect 仅调用一次加载 function - How to call loading function with React useEffect only once 反应:如何使用 useEffect 只打一次 API - React: How to hit API only once using useEffect 如何在 React 中的父组件“useEffect”之后运行 function - How to run a function just after parent component "useEffect" in React 如何使用React Apollo 2.1的Mutation组件在mount上运行变异? - How to run a mutation on mount with React Apollo 2.1's Mutation component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM