英文:
Why does the React useEffect Hook need a useState call instead of a variable?
问题
例如,从文档中(https://reactjs.org/docs/hooks-effect.html)-
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// 类似于 componentDidMount 和 componentDidUpdate:
useEffect(() => {
// 使用浏览器 API 更新文档标题
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
点我
</button>
</div>
);
}
然而,这种方式不会起作用:
import React, { useEffect } from 'react';
function Example() {
var count = 0;
// 类似于 componentDidMount 和 componentDidUpdate:
useEffect(() => {
// 使用浏览器 API 更新文档标题
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => count = (count + 1)}>
点我
</button>
</div>
);
}
需要我们使用useState调用的是什么?有什么优势?
英文:
For example, from the documentation (https://reactjs.org/docs/hooks-effect.html) -
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
However this would not work:
import React, { useEffect } from 'react';
function Example() {
var count = 0;
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => count = (count + 1)}>
Click me
</button>
</div>
);
}
What requires us to use the useState call? What are the advantages?
答案1
得分: 2
在React中,这更多涉及到变更检测的工作原理。渲染是在本地状态发生变化时触发的,可以使用像useState/useReducer这样的钩子,或者在组件的props中。
如果您不使用useState钩子来更改状态,渲染将不会触发,您将看不到视图上的变化。
useEffect将在每次渲染后执行,具体包括:
- DOM 更新
- 页面绘制完成
- useEffect清理
- useEffect执行并更改状态
- 使用新状态触发另一次渲染。
英文:
It's more related on how change detection works in React.
Rendering is triggered after changes in the local state using hooks likes useState/useReducer or in your component props.
If you don't use useState hook to change the state the rendering will not be triggered and you will not see the changes on your view
useEffect will execute after each render
- dom updated
- page painted
- useEffect cleanup
- useEffect execute and change the state
- another render is triggered with the new state
答案2
得分: 0
如果您直接修改状态,React 不会注意到更改,因此 useEffect
不会被调用。
在React中直接更改状态始终是一种不良实践,您应该始终使用 useState
的 set
方法,否则React将不会注意到更改。因此,像呈现值和 useEffect
这样的事情都不会起作用。
基本上,如果您直接更新状态,什么都不会改变。
英文:
If you modify the state directly react doesn't notice the change and therefore useEffect is not called.
Changing the state in react directly is always a bad practice you should always use the set method of useState because otherwise react will not notice the change. Therefore things like rendering the value and useEffect won't work.
Basically if you directly update the state nothing will change.
答案3
得分: 0
你需要的最简单答案是你需要一个"hook"。
在React中,"hooks"是在渲染React组件时为你提供访问不同React特性的函数。
英文:
The simplest answer is that you need a hook.
In React, hooks are functions that give you access to different React features while a React component is rendering.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论