|
1 | 1 | --- |
2 | | -title: "State: A Component's Memory" |
| 2 | +title: "State: حافظه ی یک کامپوننت" |
3 | 3 | --- |
4 | | - |
5 | 4 | <Intro> |
6 | 5 |
|
7 | | -Components often need to change what's on the screen as a result of an interaction. Typing into the form should update the input field, clicking "next" on an image carousel should change which image is displayed, clicking "buy" should put a product in the shopping cart. Components need to "remember" things: the current input value, the current image, the shopping cart. In React, this kind of component-specific memory is called *state*. |
| 6 | +کامپوننتها اغلب نیاز دارند که به خاطر تعاملات، آنچه در صفحه نمایش داده میشود را تغییر دهند. تایپ کردن در فرم باید مقدار ورودی را بهروزرسانی کند، کلیک بر روی "بعدی" در گالری تصاویر باید تصویر نمایش داده شده را تغییر دهد، کلیک بر روی "خرید" باید محصولی را به سبد خرید اضافه کند. کامپوننت ها نیاز به "یادآوری" چیزها دارند: مقدار ورودی فعلی، تصویر فعلی، سبد خرید. در React، این نوع حافظهٔ مختص به جزئیات کامپوننت به عنوان *وضعیت* شناخته میشود. |
8 | 7 |
|
9 | 8 | </Intro> |
10 | 9 |
|
11 | | -<YouWillLearn> |
| 10 | +<YouWillLearn dir='rtl'> |
12 | 11 |
|
13 | | -* How to add a state variable with the [`useState`](/reference/react/useState) Hook |
14 | | -* What pair of values the `useState` Hook returns |
15 | | -* How to add more than one state variable |
16 | | -* Why state is called local |
| 12 | +چگونه متغیر وضعیتی (state) را با استفاده از هوک ها (Hook) اضافه کنیم؟* |
| 13 | +[`useState`](/reference/react/useState) |
| 14 | +چه جفت مقداری (values) گوی (Hook) useState برمیگرداند؟* |
| 15 | +چگونه بیش از یک متغیر وضعیتی (state) اضافه کنیم؟* |
| 16 | +چرا وضعیت (state) به عنوان محلی (local) نامیده میشود؟* |
17 | 17 |
|
18 | 18 | </YouWillLearn> |
19 | 19 |
|
20 | 20 | ## When a regular variable isn’t enough {/*when-a-regular-variable-isnt-enough*/} |
21 | | - |
22 | | -Here's a component that renders a sculpture image. Clicking the "Next" button should show the next sculpture by changing the `index` to `1`, then `2`, and so on. However, this **won't work** (you can try it!): |
23 | | - |
| 21 | +<p dir='rtl'> |
| 22 | +اینجا یک کامپوننت است که یک تصویر مجسمه را نمایش میدهد. با کلیک بر روی دکمه "بعدی" باید مجسمهی بعدی را با تغییر دادن مقدار index به 1، سپس 2 و به همین ترتیب نشان دهد. با این حال، این کار نخواهد کرد (میتوانید امتحان کنید!): |
| 23 | +</p> |
| 24 | +(با دقت داخل کد را بخوانید!) |
24 | 25 | <Sandpack> |
25 | 26 |
|
26 | 27 | ```js |
@@ -150,21 +151,27 @@ button { |
150 | 151 | ``` |
151 | 152 |
|
152 | 153 | </Sandpack> |
| 154 | +<p dir='rtl'> |
| 155 | +در اینجا، رویداد handleClick یک دستگیره را به نام index بهروز میکند. اما دو مورد مانع از دیدهشدن این تغییر میشوند: |
| 156 | +1. **Local variables don't persist between renders.** |
| 157 | +متغیرهای محلی بین رندرها ثابت نمیمانند. وقتی ریاکت این- کامپوننت را بار دیگر رندر میکند، آن را از ابتدا رندر میکند، و هیچ تغییری در متغیرهای محلی را در نظر نمیگیرد. |
| 158 | + |
| 159 | +2. **Changes to local variables won't trigger renders.** |
| 160 | +تغییرات در متغیرهای محلی رندرها را فراخوانی نمیکنند-.ری اکت درک نمیکند که نیاز به دوباره رندر کردن کامپوننت با دادههای جدید دارد. |
| 161 | +برای بهروزرسانی یک کامپوننت با دادههای جدید، دو مورد نیاز است که اتفاق بیافت |
153 | 162 |
|
154 | | -The `handleClick` event handler is updating a local variable, `index`. But two things prevent that change from being visible: |
| 163 | +1. **Retain** دادهها را حفظ کنید تا بین رندرها باقی بمانند. |
| 164 | +2. **Trigger**به React علامت بزنید تا کامپوننت را با دادههای جدید رندر کند (دوباره رندر کند). |
| 165 | +گوی (Hook) useState این دو ویژگی را ارائه میدهد: |
155 | 166 |
|
156 | | -1. **Local variables don't persist between renders.** When React renders this component a second time, it renders it from scratch—it doesn't consider any changes to the local variables. |
157 | | -2. **Changes to local variables won't trigger renders.** React doesn't realize it needs to render the component again with the new data. |
| 167 | +1. A **state variable** متغیر وضعیت برای حفظ دادهها بین رندرها. |
| 168 | +2. A **state setter function** تابع تنظیمکننده وضعیت برای بهروزرسانی متغیر و ایجاد علامتی برای React تا کامپوننت را مجدداً رندر کند. |
158 | 169 |
|
159 | | -To update a component with new data, two things need to happen: |
| 170 | +[`useState`](/reference/react/useState) |
160 | 171 |
|
161 | | -1. **Retain** the data between renders. |
162 | | -2. **Trigger** React to render the component with new data (re-rendering). |
163 | 172 |
|
164 | | -The [`useState`](/reference/react/useState) Hook provides those two things: |
165 | 173 |
|
166 | | -1. A **state variable** to retain the data between renders. |
167 | | -2. A **state setter function** to update the variable and trigger React to render the component again. |
| 174 | +</p> |
168 | 175 |
|
169 | 176 | ## Adding a state variable {/*adding-a-state-variable*/} |
170 | 177 |
|
|
0 commit comments