Skip to content

Commit f4e820a

Browse files
authored
Merge pull request #168 from nguyenary/translate-component-state-page
translate: faq component state page
2 parents 93e6cd7 + be5f4c7 commit f4e820a

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

content/docs/faq-state.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,101 +6,101 @@ layout: docs
66
category: FAQ
77
---
88

9-
### What does `setState` do? {#what-does-setstate-do}
9+
### `setState` để làm gì? {#what-does-setstate-do}
1010

11-
`setState()` schedules an update to a component's `state` object. When state changes, the component responds by re-rendering.
11+
`setState()` lên lịch cập nhật cho một object `state` của component. Khi state thay đổi, component phản hồi bằng cách hiển thị lại.
1212

13-
### What is the difference between `state` and `props`? {#what-is-the-difference-between-state-and-props}
13+
### Sự khác biệt giữa `state` `props`? {#what-is-the-difference-between-state-and-props}
1414

15-
[`props`](/docs/components-and-props.html) (short for "properties") and [`state`](/docs/state-and-lifecycle.html) are both plain JavaScript objects. While both hold information that influences the output of render, they are different in one important way: `props` get passed *to* the component (similar to function parameters) whereas `state` is managed *within* the component (similar to variables declared within a function).
15+
[`props`](/docs/components-and-props.html) (viết tắt của "properties") [`state`](/docs/state-and-lifecycle.html) đều là các object JavaScript đơn giản. Mặc dù cả hai đều nắm giữ thông tin ảnh hưởng đến kết quả hiển thị, nhưng chúng khác nhau ở một điểm quan trọng: `props` được chuyển *cho* component (tương tự như các tham số function) nhưng trái lại `state` được quản lý *trong* component (tương tự như các biến được khai báo trong một function).
1616

17-
Here are some good resources for further reading on when to use `props` vs `state`:
18-
* [Props vs State](https://github.com/uberVU/react-guide/blob/master/props-vs-state.md)
19-
* [ReactJS: Props vs. State](https://lucybain.com/blog/2016/react-state-vs-pros/)
17+
Dưới đây là một số nguồn tốt để đọc thêm về thời điểm sử dụng `props` so với `state`:
18+
* [Props với State](https://github.com/uberVU/react-guide/blob/master/props-vs-state.md)
19+
* [ReactJS: Props với State](https://lucybain.com/blog/2016/react-state-vs-pros/)
2020

21-
### Why is `setState` giving me the wrong value? {#why-is-setstate-giving-me-the-wrong-value}
21+
### Tại sao `setState` lại cho tôi giá trị sai? {#why-is-setstate-giving-me-the-wrong-value}
2222

23-
In React, both `this.props` and `this.state` represent the *rendered* values, i.e. what's currently on the screen.
23+
Trong React, cả `this.props` `this.state` đều đại diện cho các giá trị *được hiển thị*, tức là những gì hiện có trên màn hình.
2424

25-
Calls to `setState` are asynchronous - don't rely on `this.state` to reflect the new value immediately after calling `setState`. Pass an updater function instead of an object if you need to compute values based on the current state (see below for details).
25+
Các lệnh gọi đến `setState` là bất đồng bộ - đừng dựa vào `this.state` để phản ánh giá trị mới sau khi gọi `setState`. Truyền một function cập nhật thay vì một object nếu bạn cần tính toán các giá trị dựa trên state hiện tại (xem chi tiết bên dưới).
2626

27-
Example of code that will *not* behave as expected:
27+
Ví dụ về code sẽ *không* hoạt động như mong đợi:
2828

2929
```jsx
3030
incrementCount() {
31-
// Note: this will *not* work as intended.
31+
// Ghi chú: điều này sẽ *không* hoạt động như dự định.
3232
this.setState({count: this.state.count + 1});
3333
}
3434

3535
handleSomething() {
36-
// Let's say `this.state.count` starts at 0.
36+
// Giả sử `this.state.count` bắt đầu 0.
3737
this.incrementCount();
3838
this.incrementCount();
3939
this.incrementCount();
40-
// When React re-renders the component, `this.state.count` will be 1, but you expected 3.
40+
// Khi React hiển thị lại component, `this.state.count` sẽ là 1, bạn mong muốn là 3.
4141

42-
// This is because `incrementCount()` function above reads from `this.state.count`,
43-
// but React doesn't update `this.state.count` until the component is re-rendered.
44-
// So `incrementCount()` ends up reading `this.state.count` as 0 every time, and sets it to 1.
42+
// Điều này do hàm `incrementCount()` ở trên đọc từ `this.state.count`,
43+
// nhưng React không cập nhật `this.state.count` cho đến khi thành phần được hiển thị lại.
44+
// Vì vậy `incrementCount()` kết thúc bằng cách đọc `this.state.count` 0 mỗi lần, và đặt nó thành 1.
4545

46-
// The fix is described below!
46+
// Cách khắc phục được mô tả bên dưới!
4747
}
4848
```
4949

50-
See below for how to fix this problem.
50+
Xem bên dưới để biết cách khắc phục sự cố này.
5151

52-
### How do I update state with values that depend on the current state? {#how-do-i-update-state-with-values-that-depend-on-the-current-state}
52+
### Làm cách nào để cập nhật state với các giá trị phụ thuộc vào state hiện tại? {#how-do-i-update-state-with-values-that-depend-on-the-current-state}
5353

54-
Pass a function instead of an object to `setState` to ensure the call always uses the most updated version of state (see below).
54+
Truyền một function thay vì một object vào `setState` để đảm bảo cuộc gọi luôn sử dụng state cập nhật mới nhất (xem bên dưới).
5555

56-
### What is the difference between passing an object or a function in `setState`? {#what-is-the-difference-between-passing-an-object-or-a-function-in-setstate}
56+
### Sự khác biệt giữa truyền một object hoặc một function trong `setState` là gì? {#what-is-the-difference-between-passing-an-object-or-a-function-in-setstate}
5757

58-
Passing an update function allows you to access the current state value inside the updater. Since `setState` calls are batched, this lets you chain updates and ensure they build on top of each other instead of conflicting:
58+
Chuyển một function cập nhật cho phép bạn truy cập giá trị trạng thái hiện tại bên trong trình cập nhật. Vì các lệnh gọi `setState` được thực hiện theo đợt, điều này cho phép bạn cập nhật chuỗi và đảm bảo chúng xây dựng dựa trên nhau thay vì xung đột:
5959

6060
```jsx
6161
incrementCount() {
6262
this.setState((state) => {
63-
// Important: read `state` instead of `this.state` when updating.
63+
// Quan trọng: đọc `state` thay vì` this.state` khi cập nhật.
6464
return {count: state.count + 1}
6565
});
6666
}
6767

6868
handleSomething() {
69-
// Let's say `this.state.count` starts at 0.
69+
// Giả sử `this.state.count` bắt đầu từ 0.
7070
this.incrementCount();
7171
this.incrementCount();
7272
this.incrementCount();
7373

74-
// If you read `this.state.count` now, it would still be 0.
75-
// But when React re-renders the component, it will be 3.
74+
// Nếu bây giờ bạn đọc `this.state.count`, nó vẫn là 0.
75+
// Nhưng khi React hiển thị component, nó sẽ là 3.
7676
}
7777
```
7878

79-
[Learn more about setState](/docs/react-component.html#setstate)
79+
[Tìm hiểu thêm về setState](/docs/react-component.html#setstate)
8080

81-
### When is `setState` asynchronous? {#when-is-setstate-asynchronous}
81+
### Khi nào `setState` bất đồng bộ? {#when-is-setstate-asynchronous}
8282

83-
Currently, `setState` is asynchronous inside event handlers.
83+
Hiện tại, `setState` là bất đồng bộ bên trong các trình xử lý sự kiện.
8484

85-
This ensures, for example, that if both `Parent` and `Child` call `setState` during a click event, `Child` isn't re-rendered twice. Instead, React "flushes" the state updates at the end of the browser event. This results in significant performance improvements in larger apps.
85+
Ví dụ, điều này đảm bảo rằng nếu cả `Cha``Con` gọi `setState` trong một sự kiện nhấp chuột, `Con` con không được hiển thị lại 2 lần. Thay vào đó, React “xóa” các cập nhật state vào cuối sự kiện trình duyệt. Điều này dẫn đến cải thiện hiệu suất đáng kể trong các ứng dụng lớn hơn.
8686

87-
This is an implementation detail so avoid relying on it directly. In the future versions, React will batch updates by default in more cases.
87+
Đây là một chi tiết hoàn thiện nên tránh dựa dẫm vào nó trực tiếp. Trong các phiên bản tương lai, React sẽ cập nhật hàng loạt theo mặc định trong nhiều trường hợp hơn.
8888

89-
### Why doesn't React update `this.state` synchronously? {#why-doesnt-react-update-thisstate-synchronously}
89+
### Tại sao React không cập nhật `this.state` một cách đồng bộ? {#why-doesnt-react-update-thisstate-synchronously}
9090

91-
As explained in the previous section, React intentionally "waits" until all components call `setState()` in their event handlers before starting to re-render. This boosts performance by avoiding unnecessary re-renders.
91+
Như đã giải thích trong phần trước, React chủ ý "đợi" cho đến khi tất cả các thành phần gọi `setState()` trong trình xử lý sự kiện của họ trước khi bắt đầu hiển thị lại. Điều này tăng hiệu suất bằng cách tránh các hiển thị không cần thiết.
9292

93-
However, you might still be wondering why React doesn't just update `this.state` immediately without re-rendering.
93+
Tuy nhiên, bạn vẫn có thể thắc mắc tại sao React không cập nhật `this.state` ngay lập tức mà không hiển thị lại.
9494

95-
There are two main reasons:
95+
Có hai lý do chính:
9696

97-
* This would break the consistency between `props` and `state`, causing issues that are very hard to debug.
98-
* This would make some of the new features we're working on impossible to implement.
97+
* Điều này sẽ phá vỡ sự nhất quán giữa `props` `state`, gây ra các vấn đề rất khó gỡ lỗi.
98+
* Điều này sẽ khiến một số tính năng mới mà chúng tôi đang nghiên cứu không thể triển khai.
9999

100-
This [GitHub comment](https://github.com/facebook/react/issues/11527#issuecomment-360199710) dives deep into the specific examples.
100+
[Nhận xét Github](https://github.com/facebook/react/issues/11527#issuecomment-360199710) này đi sâu vào các ví dụ cụ thể.
101101

102-
### Should I use a state management library like Redux or MobX? {#should-i-use-a-state-management-library-like-redux-or-mobx}
102+
### Tôi có nên sử dụng thư viện quản lý state như Redux hoặc MobX không? {#should-i-use-a-state-management-library-like-redux-or-mobx}
103103

104-
[Maybe.](https://redux.js.org/faq/general#when-should-i-use-redux)
104+
[Có lẽ.](https://redux.js.org/faq/general#when-should-i-use-redux)
105105

106-
It's a good idea to get to know React first, before adding in additional libraries. You can build quite complex applications using only React.
106+
Bạn nên làm quen với React trước, trước khi sử dụng các thư viện bổ sung. Bạn có thể xây dựng các ứng dụng khá phức tạp chỉ bằng React.

0 commit comments

Comments
 (0)