You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/reacting-to-input-with-state.md
+34-48Lines changed: 34 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -479,21 +479,20 @@ function submitForm(answer) {
479
479
* برنامه نویسی اعلانی به معنای توصیف رابط کاربری برای هر state بصری به جای مدیریت جزئیات رابط کاربری است (دستوری).
480
480
* هنگام توسعه یک کامپوننت:
481
481
1. تمامی state های بصری آن را شناسایی کنید.
482
-
2.
483
-
2. Determine the human and computer triggers for state changes.
484
-
3. Model the state with `useState`.
485
-
4. Remove non-essential state to avoid bugs and paradoxes.
486
-
5. Connect the event handlers to set state.
482
+
2. تغییرات state توسط عاملهای انسانی و کامپیوتری را تعیین کنید.
483
+
3. state را با استفاده از `useState` مدل کنید.
484
+
4. state های غیرضروری را به منظور اجتناب از باگها و تناقضات حذف کنید.
485
+
5. Event handler ها را برای تنظیم state متصل کنید.
487
486
488
487
</Recap>
489
488
490
489
<Challenges>
491
490
492
-
#### Add and remove a CSS class {/*add-and-remove-a-css-class*/}
491
+
#### حذف و اضافه کردن یک کلاس CSS {/*add-and-remove-a-css-class*/}
493
492
494
-
Make it so that clicking on the picture *removes* the `background--active`CSS class from the outer `<div>`, but *adds* the `picture--active`class to the `<img>`. Clicking the background again should restore the original CSS classes.
495
-
496
-
Visually, you should expect that clicking on the picture removes the purple background and highlights the picture border. Clicking outside the picture highlights the background, but removes the picture border highlight.
493
+
طوری برنامه ریزی کنید که با کلیک روی تصویر کلاس CSS `background--active`از `<div>` بیرونی *حذف شود*، اما کلاس `picture--active`به `<img>` *اضافه شود*.
494
+
کلیک مجدد برروی پس زمینه باید کلاسهای اصلی CSS را بازیابی کند.
495
+
از نظر بصری، انتظار می رود که با کلیک روی تصویر،پس زمینه بنفش حذف شود و حاشیه تصویر برجسته شود. کلیک خارج از تصویر باعث برجسته سازی پس زمینه می شود، اما برجستگی حاشیه تصویر را حذف می کند.
این کامپوننت دو نوع state بصری دارد: زمانی که تصویر فعال است، و زمانی که تصویر غیرفعال است:
544
544
545
-
This component has two visual states: when the image is active, and when the image is inactive:
546
-
547
-
* When the image is active, the CSS classes are `background` and `picture picture--active`.
548
-
* When the image is inactive, the CSS classes are `background background--active` and `picture`.
549
-
550
-
A single boolean state variable is enough to remember whether the image is active. The original task was to remove or add CSS classes. However, in React you need to *describe* what you want to see rather than *manipulate* the UI elements. So you need to calculate both CSS classes based on the current state. You also need to [stop the propagation](/learn/responding-to-events#stopping-propagation) so that clicking the image doesn't register as a click on the background.
545
+
* زمانی که تصویر فعال است، کلاسهای CSS، `background` و `picture picture--active` هستند.
546
+
* زمانی که تصویر غیرفعال است، کلاسهای CSS، `background background--active` و `picture` هستند.
547
+
یک متغیر state بولین به تنهایی کافی است تا فعال یا غیرفعال بودن تصویر را نگهداری کند. وظیفه اصلی حذف یا اضافه کردن کلاسهای CSS بود. اگرچه، در ری اکت شما نیاز دارید به جای آنکه المنت های رابط کاربری را *دستکاری کنید* آنچه که می خواهید ببینید را *توصیف کنید*. بنابراین لازم است که هر دو کلاس CSS را براساس state فعلی محاسبه کنید. همچنین شما باید [انتشار را متوقف کنید](/learn/responding-to-events#stopping-propagation) تا کلیک روی تصویر به عنوان کلیک بر روی پس زمینه ثبت نگردد.
551
548
552
-
Verify that this version works by clicking the image and then outside of it:
549
+
بررسی کنید که این نسخه با کلیک بر روی تصویر و سپس خارج از آن کار میکند:
Keep in mind that if two different JSX chunks describe the same tree, their nesting (first `<div>` → first `<img>`) has to line up. Otherwise, toggling `isActive` would recreate the whole tree below and [reset its state.](/learn/preserving-and-resetting-state) This is why, if a similar JSX tree gets returned in both cases, it is better to write them as a single piece of JSX.
681
+
بخاطر داشته باشید که اگر دو قطعه مختلف JSX یک درخت مشابه را توصیف کنند، تودرتویی آنها (اولین `<div>` → اولین `<img>`) باید منظم باشد.
682
+
درغیراینصورت، تغییر وضعیت `isActive` باعث بازسازی کل زیردرخت شده و [state آن را بازنشانی می کند.](/learn/preserving-and-resetting-state) به این دلیل است که اگر یک درخت JSX مشابه در هردو حالت بازگردانده شود، بهتر است آنها را به عنوان یک قطعه JSX واحد بنویسید.
687
683
688
684
</Solution>
685
+
#### ویرایشگر پروفایل {/*profile-editor*/}
689
686
690
-
#### Profile editor {/*profile-editor*/}
691
-
692
-
Here is a small form implemented with plain JavaScript and DOM. Play with it to understand its behavior:
693
-
687
+
در زیر یک فرم کوچک با استفاده از جاوااسکریپت خام و DOM پیاده سازی شده است. با آن بازی کنید تا رفتار آن را درک کنید:
این فرم بین دو حالت جابجا میشود: در حالت ویرایش، ورودیها را مشاهده میکنید و در حالت مشاهده، تنها نتیجه را مشاهده میکنید. عنوان دکمه براساس حالتی که در آن قرار دارید بین "ویرایش" و "ذخیره" تغییر می کند. وقتی که ورودی ها را تغییر می دهید، پیام خوشامدگویی در پایین، به شکل بلادرنگ بروزرسانی می شود.
783
+
وظیفه شما این است که آن را در محیط تستی ری اکت زیر دوباره پیاده سازی کنید. برای راحتی شما، نشانه گذاری از قبل به JSX تبدیل شده است، اما شما باید ورودی ها را مانند نسخه اصلی نمایش داده و پنهان کنید.
788
784
789
-
This form switches between two modes: in the editing mode, you see the inputs, and in the viewing mode, you only see the result. The button label changes between "Edit" and "Save" depending on the mode you're in. When you change the inputs, the welcome message at the bottom updates in real time.
790
-
791
-
Your task is to reimplement it in React in the sandbox below. For your convenience, the markup was already converted to JSX, but you'll need to make it show and hide the inputs like the original does.
792
-
793
-
Make sure that it updates the text at the bottom, too!
You will need two state variables to hold the input values: `firstName` and `lastName`. You're also going to need an `isEditing` state variable that holds whether to display the inputs or not. You should _not_ need a `fullName` variable because the full name can always be calculated from the `firstName` and the `lastName`.
829
-
830
-
Finally, you should use [conditional rendering](/learn/conditional-rendering) to show or hide the inputs depending on `isEditing`.
819
+
شما به دو متغیر state برای نگهداری مقادیر ورودی نیاز خواهی داشت: `firstName` و `lastName`. همچنین شما به یک متغیر state `isEditing` نیاز دارید که نگهدارنده وضعیت نمایش یا عدم نمایش ورودی ها باشد. شما _نباید_ به یک متغیر `fullName` نیاز داشته باشید چرا که نام کامل همیشه از `firstName` و `lastName` به دست می آید.
820
+
درنهایت، شما باید از [رندر کردن شرطی](/learn/conditional-rendering) برای نمایش یا مخفی کردن ورودی ها براساس `isEditing` استفاده کنید.
تصور کنید ری اکت وجود نداشت. آیا می توانید این کد را به گونه ای بازسازی کنید که منطق با حساسیت کمتر و بیشتر شبیه نسخه ری اکت شود؟
990
978
991
-
Imagine React didn't exist. Can you refactor this code in a way that makes the logic less fragile and more similar to the React version? What would it look like if the state was explicit, like in React?
992
-
993
-
If you're struggling to think where to start, the stub below already has most of the structure in place. If you start here, fill in the missing logic in the `updateDOM` function. (Refer to the original code where needed.)
979
+
اگر مانند ری اکت state به صورت صریح باشد، به چه شکل خواهد بود؟
980
+
اگر برای شروع درحال تقلا هستید، بخش زیر هم اکنون بیشتر ساختار را در خود جای داده است. اگر از اینجا شروع می کنید، مطق جاافتاده در تابع `updateDOM` تکمیل کنید. (برای اطلاعات بیشتر به کد اصلی مراجعه کنید.)
994
981
995
982
<Sandpack>
996
983
@@ -1030,12 +1017,12 @@ function setIsEditing(value) {
تابع `updateDOM` که نوشتید نشان می دهد وقتی که شما state را ست می کنید ری اکت در پس زمینه چه کاری انجام می دهد. (اگرچه، ری اکت از تغییر ویژگیهای DOM پس از آخرین مرتبه ست شدن تغییری نداشته اند اجتناب می کند.)
1217
1203
The `updateDOM` function you wrote shows what React does under the hood when you set the state. (However, React also avoids touching the DOM for properties that have not changed since the last time they were set.)
0 commit comments