Skip to content

Commit 65ad424

Browse files
committed
feat: 2022.08.23
1 parent 1d0ab8a commit 65ad424

3 files changed

Lines changed: 930 additions & 4 deletions

Cute-Vue/Blog/11 个需要避免的 React 错误用法.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ useEffect(() => {
216216

217217
### 问题描述
218218

219-
我们在类组件中,经常使用 `componentDidMount()` 生命周期方法去清理一些副作用,比如定时器、事件监听等。
219+
我们在类组件中,经常使用 `componentWillUnmount()` 生命周期方法去清理一些副作用,比如定时器、事件监听等。
220220

221221
### 解决方法
222222

223-
可以为 `useEffect()`的副作用函数设置返回函数,该函数类似 `componentDidMount()` 生命周期方法的作用:
223+
可以为 `useEffect()`的副作用函数设置返回函数,该函数类似 `componentWillUnmount()` 生命周期方法的作用:
224224

225225
```jsx
226226
useEffect(() => {
@@ -229,6 +229,21 @@ useEffect(() => {
229229
}, [name, age]);
230230
```
231231

232+
另外,当需要实现`componentWillUnmount` 生命周期函数的效果时,可以在 `useEffect()`函数返回一个函数即可,该函数会在组件卸载时执行:
233+
234+
```jsx
235+
export default function App() {
236+
const [count, setCount] = useState(0);
237+
useEffect(() => {
238+
setCount(count + 1);
239+
return () => {
240+
console.log("[组件已卸载]");
241+
};
242+
}, []);
243+
return <div className="App">{count}</div>;
244+
}
245+
```
246+
232247
### 文档介绍
233248

234249
[React - Example Using Hooks](https://reactjs.org/docs/hooks-effect.html#example-using-hooks-1)

Cute-Vue/Blog/Top 11 Mistakes to Avoid When Using React In 2022.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const listItems = numbers.map((number, index) => <li key={index}>{number}</li>);
4242

4343
`key` helps React identify which elements have changed, such as been added or removed. So we need to set a unique `key` value for each element in the array.
4444

45+
For the value of `key`, it is best to set it to a unique value. In the above example, `index` is used as the value of `key`. Officially, it is not recommended. The order of the list will change, and there is no unique value or a last resort. In this case, performance will be degraded.
46+
4547
### Documentation
4648

4749
[React - Basic List Component](https://reactjs.org/docs/lists-and-keys.html#basic-list-component)
@@ -217,11 +219,11 @@ useEffect(() => {
217219

218220
### Problem
219221

220-
In class components, we use the `componentDidMount()` lifecycle method to clean up some side effects, such as timers, event listeners, etc.
222+
In class components, we use the `componentWillUnmount()` lifecycle method to clean up some side effects, such as timers, event listeners, etc.
221223

222224
### Solutions
223225

224-
A return function can be set for the side effect function of `useEffect()`, which is similar to the role of the `componentDidMount()` lifecycle method:
226+
A return function can be set for the side effect function of `useEffect()`, which is similar to the role of the `componentWillUnmount()` lifecycle method:
225227

226228
```jsx
227229
useEffect(() => {

0 commit comments

Comments
 (0)