diff --git a/src/content/learn/sharing-state-between-components.md b/src/content/learn/sharing-state-between-components.md index 2fbef8918..2c1eafd09 100644 --- a/src/content/learn/sharing-state-between-components.md +++ b/src/content/learn/sharing-state-between-components.md @@ -1,31 +1,31 @@ --- -title: Sharing State Between Components +title: مشاركة الحالة بين المكونات --- -Sometimes, you want the state of two components to always change together. To do it, remove state from both of them, move it to their closest common parent, and then pass it down to them via props. This is known as *lifting state up,* and it's one of the most common things you will do writing React code. +أحيانًا، تريد أن تتغير حالة مكوّنين دائمًا معًا. لتحقيق ذلك، أزِل الحالة من كليهما، وانقلها إلى أقرب مكوّن أب مشترك لهما، ثم مرّرها إليهما عبر الخصائص (props). يُعرف هذا بـ *رفع الحالة لمستوى أعلى (lifting state up)*، وهو من أكثر الأمور شيوعًا التي ستفعلها عند كتابة كود React. -- How to share state between components by lifting it up -- What are controlled and uncontrolled components +- كيفية مشاركة الحالة بين المكونات عن طريق رفعها لأعلى +- ما المقصود بالمكوّنات المُتحكَّم فيها (controlled) وغير المُتحكَّم فيها (uncontrolled) -## Lifting state up by example {/*lifting-state-up-by-example*/} +## رفع الحالة لمستوى أعلى: مثال {/*lifting-state-up-by-example*/} -In this example, a parent `Accordion` component renders two separate `Panel`s: +في هذا المثال، يعرض المكوّن الأب `Accordion` لوحتين منفصلتين `Panel`: * `Accordion` - `Panel` - `Panel` -Each `Panel` component has a boolean `isActive` state that determines whether its content is visible. +لكل مكوّن `Panel` حالة منطقية `isActive` تحدد ما إذا كان محتواه ظاهرًا. -Press the Show button for both panels: +اضغط على زر "إظهار" في اللوحتين: @@ -41,7 +41,7 @@ function Panel({ title, children }) {

{children}

) : ( )} @@ -51,12 +51,12 @@ function Panel({ title, children }) { export default function Accordion() { return ( <> -

Almaty, Kazakhstan

- - With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city. +

ألماتي، كازاخستان

+ + يبلغ عدد سكانها نحو 2 مليون نسمة، وهي أكبر مدن كازاخستان. وكانت عاصمة البلاد من عام 1929 حتى عام 1997. - - The name comes from алма, the Kazakh word for "apple" and is often translated as "full of apples". In fact, the region surrounding Almaty is thought to be the ancestral home of the apple, and the wild Malus sieversii is considered a likely candidate for the ancestor of the modern domestic apple. + + الاسم مأخوذ من алма، وهي كلمة كازاخية تعني "تفاحة"، وكثيرًا ما تُترجم بمعنى "مليئة بالتفاح". والمنطقة المحيطة بألماتي يُعتقد أنها الموطن الأصلي للتفاح، ويُعدّ النوع البري Malus sieversii من أرجح أسلاف التفاح المنزلي الحديث. ); @@ -64,6 +64,7 @@ export default function Accordion() { ``` ```css +body { direction: rtl; } h3, p { margin: 5px 0px; } .panel { padding: 10px; @@ -73,59 +74,59 @@ h3, p { margin: 5px 0px; }
-Notice how pressing one panel's button does not affect the other panel--they are independent. +لاحظ أن الضغط على زر لوحة لا يؤثر على اللوحة الأخرى، فهما مستقلتان. - + -Initially, each `Panel`'s `isActive` state is `false`, so they both appear collapsed +في البداية، حالة `isActive` لكل `Panel` تساوي `false`، لذا تظهر كلتاهما مطوية - + -Clicking either `Panel`'s button will only update that `Panel`'s `isActive` state alone +النقر على زر أي `Panel` يحدّث حالة `isActive` الخاصة به فقط -**But now let's say you want to change it so that only one panel is expanded at any given time.** With that design, expanding the second panel should collapse the first one. How would you do that? +**لكن لنفترض الآن أنك تريد تغيير السلوك بحيث تكون لوحة واحدة فقط مفتوحة في أي وقت.** مع هذا التصميم، فتح اللوحة الثانية يجب أن يطوي الأولى. كيف يمكنك فعل ذلك؟ -To coordinate these two panels, you need to "lift their state up" to a parent component in three steps: +لتنسيق عمل هاتين اللوحتين، تحتاج إلى "رفع حالتهما لمستوى أعلى" إلى مكوّن أب في ثلاث خطوات: -1. **Remove** state from the child components. -2. **Pass** hardcoded data from the common parent. -3. **Add** state to the common parent and pass it down together with the event handlers. +1. **أزِل** الحالة من المكوّنات الابنة. +2. **مرّر** بيانات ثابتة من المكوّن الأب المشترك. +3. **أضِف** الحالة إلى المكوّن الأب المشترك، ومرّرها لأسفل مع معالجات الأحداث. -This will allow the `Accordion` component to coordinate both `Panel`s and only expand one at a time. +هذا سيمكّن المكوّن `Accordion` من تنسيق عمل اللوحتين، وتوسيع لوحة واحدة فقط في كل مرة. -### Step 1: Remove state from the child components {/*step-1-remove-state-from-the-child-components*/} +### الخطوة 1: إزالة الحالة من المكوّنات الابنة {/*step-1-remove-state-from-the-child-components*/} -You will give control of the `Panel`'s `isActive` to its parent component. This means that the parent component will pass `isActive` to `Panel` as a prop instead. Start by **removing this line** from the `Panel` component: +ستمنح التحكم في `isActive` الخاص بـ `Panel` إلى مكوّنه الأب. وهذا يعني أن المكوّن الأب سيمرر `isActive` إلى `Panel` كخاصية بدلًا من ذلك. ابدأ بـ **حذف هذا السطر** من المكوّن `Panel`: ```js const [isActive, setIsActive] = useState(false); ``` -And instead, add `isActive` to the `Panel`'s list of props: +وبدلًا من ذلك، أضِف `isActive` إلى قائمة الخصائص الخاصة بـ `Panel`: ```js function Panel({ title, children, isActive }) { ``` -Now the `Panel`'s parent component can *control* `isActive` by [passing it down as a prop.](/learn/passing-props-to-a-component) Conversely, the `Panel` component now has *no control* over the value of `isActive`--it's now up to the parent component! +الآن يمكن للمكوّن الأب أن *يتحكم* في `isActive` عن طريق [تمريره كخاصية.](/learn/passing-props-to-a-component) في المقابل، لم يعد للمكوّن `Panel` *أي تحكم* في قيمة `isActive`، فالأمر أصبح بيد المكوّن الأب! -### Step 2: Pass hardcoded data from the common parent {/*step-2-pass-hardcoded-data-from-the-common-parent*/} +### الخطوة 2: تمرير بيانات ثابتة من المكوّن الأب المشترك {/*step-2-pass-hardcoded-data-from-the-common-parent*/} -To lift state up, you must locate the closest common parent component of *both* of the child components that you want to coordinate: +لرفع الحالة لمستوى أعلى، يجب أن تحدد أقرب مكوّن أب مشترك لـ *كلا* المكوّنين الابنين اللذين تريد تنسيق عملهما: -* `Accordion` *(closest common parent)* +* `Accordion` *(أقرب مكوّن أب مشترك)* - `Panel` - `Panel` -In this example, it's the `Accordion` component. Since it's above both panels and can control their props, it will become the "source of truth" for which panel is currently active. Make the `Accordion` component pass a hardcoded value of `isActive` (for example, `true`) to both panels: +في هذا المثال، هذا المكوّن هو `Accordion`. بما أنه أعلى من اللوحتين ويستطيع التحكم في خصائصهما، فسيصبح "مصدر الحقيقة" لتحديد اللوحة النشطة حاليًا. اجعل المكوّن `Accordion` يمرر قيمة ثابتة لـ `isActive` (مثلًا `true`) إلى اللوحتين: @@ -135,12 +136,12 @@ import { useState } from 'react'; export default function Accordion() { return ( <> -

Almaty, Kazakhstan

- - With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city. +

ألماتي، كازاخستان

+ + يبلغ عدد سكانها نحو 2 مليون نسمة، وهي أكبر مدن كازاخستان. وكانت عاصمة البلاد من عام 1929 حتى عام 1997. - - The name comes from алма, the Kazakh word for "apple" and is often translated as "full of apples". In fact, the region surrounding Almaty is thought to be the ancestral home of the apple, and the wild Malus sieversii is considered a likely candidate for the ancestor of the modern domestic apple. + + الاسم مأخوذ من алма، وهي كلمة كازاخية تعني "تفاحة"، وكثيرًا ما تُترجم بمعنى "مليئة بالتفاح". والمنطقة المحيطة بألماتي يُعتقد أنها الموطن الأصلي للتفاح، ويُعدّ النوع البري Malus sieversii من أرجح أسلاف التفاح المنزلي الحديث. ); @@ -154,7 +155,7 @@ function Panel({ title, children, isActive }) {

{children}

) : ( )} @@ -163,6 +164,7 @@ function Panel({ title, children, isActive }) { ``` ```css +body { direction: rtl; } h3, p { margin: 5px 0px; } .panel { padding: 10px; @@ -172,21 +174,21 @@ h3, p { margin: 5px 0px; }
-Try editing the hardcoded `isActive` values in the `Accordion` component and see the result on the screen. +جرّب تعديل القيم الثابتة لـ `isActive` في المكوّن `Accordion`، ولاحظ النتيجة على الشاشة. -### Step 3: Add state to the common parent {/*step-3-add-state-to-the-common-parent*/} +### الخطوة 3: إضافة الحالة إلى المكوّن الأب المشترك {/*step-3-add-state-to-the-common-parent*/} -Lifting state up often changes the nature of what you're storing as state. +غالبًا ما يغيّر رفع الحالة لمستوى أعلى طبيعة ما تخزنه كحالة. -In this case, only one panel should be active at a time. This means that the `Accordion` common parent component needs to keep track of *which* panel is the active one. Instead of a `boolean` value, it could use a number as the index of the active `Panel` for the state variable: +في هذه الحالة، ينبغي أن تكون لوحة واحدة فقط نشطة في كل مرة. وهذا يعني أن المكوّن الأب المشترك `Accordion` يحتاج إلى تتبّع *أي* لوحة هي النشطة. وبدلًا من قيمة منطقية (`boolean`)، يمكنه استخدام رقم كفهرس (index) للوحة النشطة في متغير الحالة: ```js const [activeIndex, setActiveIndex] = useState(0); ``` -When the `activeIndex` is `0`, the first panel is active, and when it's `1`, it's the second one. +عندما تكون `activeIndex` تساوي `0`، تكون اللوحة الأولى نشطة، وعندما تساوي `1`، تكون اللوحة الثانية نشطة. -Clicking the "Show" button in either `Panel` needs to change the active index in `Accordion`. A `Panel` can't set the `activeIndex` state directly because it's defined inside the `Accordion`. The `Accordion` component needs to *explicitly allow* the `Panel` component to change its state by [passing an event handler down as a prop](/learn/responding-to-events#passing-event-handlers-as-props): +النقر على زر "إظهار" في أي من مكوّنات `Panel` يجب أن يغيّر الفهرس النشط في `Accordion`. لا يستطيع `Panel` تعيين `activeIndex` مباشرة لأنها معرّفة داخل `Accordion`. لذلك يجب أن يسمح المكوّن `Accordion` *صراحةً* للمكوّن `Panel` بتغيير حالته، وذلك بـ [تمرير معالج حدث كخاصية](/learn/responding-to-events#passing-event-handlers-as-props): ```js <> @@ -205,7 +207,7 @@ Clicking the "Show" button in either `Panel` needs to change the active index in ``` -The ` )} @@ -257,6 +259,7 @@ function Panel({ ``` ```css +body { direction: rtl; } h3, p { margin: 5px 0px; } .panel { padding: 10px; @@ -266,19 +269,19 @@ h3, p { margin: 5px 0px; } -This completes lifting state up! Moving state into the common parent component allowed you to coordinate the two panels. Using the active index instead of two "is shown" flags ensured that only one panel is active at a given time. And passing down the event handler to the child allowed the child to change the parent's state. +بهذا تكتمل عملية رفع الحالة لمستوى أعلى! نقل الحالة إلى المكوّن الأب المشترك مكّننا من تنسيق عمل اللوحتين. واستخدام الفهرس النشط بدلًا من علامتين منفصلتين "معروضة" ضمن أن تكون لوحة واحدة فقط نشطة في أي وقت. وتمرير معالج الحدث إلى المكوّن الابن أتاح له تغيير حالة المكوّن الأب. - + -Initially, `Accordion`'s `activeIndex` is `0`, so the first `Panel` receives `isActive = true` +في البداية، قيمة `activeIndex` في `Accordion` هي `0`، لذا يستقبل المكوّن `Panel` الأول القيمة `isActive = true` - + -When `Accordion`'s `activeIndex` state changes to `1`, the second `Panel` receives `isActive = true` instead +عندما تتغير حالة `activeIndex` في `Accordion` إلى `1`، يستقبل المكوّن `Panel` الثاني القيمة `isActive = true` بدلًا من ذلك @@ -286,48 +289,48 @@ When `Accordion`'s `activeIndex` state changes to `1`, the second `Panel` receiv -#### Controlled and uncontrolled components {/*controlled-and-uncontrolled-components*/} +#### المكوّنات المُتحكَّم فيها وغير المُتحكَّم فيها {/*controlled-and-uncontrolled-components*/} -It is common to call a component with some local state "uncontrolled". For example, the original `Panel` component with an `isActive` state variable is uncontrolled because its parent cannot influence whether the panel is active or not. +من الشائع أن نصف مكوّنًا يملك بعض الحالة المحلية بأنه "غير مُتحكَّم فيه" (uncontrolled). فعلى سبيل المثال، المكوّن `Panel` الأصلي الذي يملك متغير الحالة `isActive` غير مُتحكَّم فيه، لأن مكوّنه الأب لا يستطيع التأثير في ما إذا كانت اللوحة نشطة أم لا. -In contrast, you might say a component is "controlled" when the important information in it is driven by props rather than its own local state. This lets the parent component fully specify its behavior. The final `Panel` component with the `isActive` prop is controlled by the `Accordion` component. +في المقابل، يمكن أن نقول إن المكوّن "مُتحكَّم فيه" عندما تكون المعلومات المهمة فيه مدفوعة بالخصائص (props) وليس بحالته المحلية. وهذا يتيح للمكوّن الأب تحديد سلوكه بالكامل. فالمكوّن `Panel` النهائي الذي يملك الخاصية `isActive` مُتحكَّم فيه من قِبل المكوّن `Accordion`. -Uncontrolled components are easier to use within their parents because they require less configuration. But they're less flexible when you want to coordinate them together. Controlled components are maximally flexible, but they require the parent components to fully configure them with props. +المكوّنات غير المُتحكَّم فيها أسهل في الاستخدام داخل مكوّناتها الأب لأنها تحتاج إلى إعداد أقل، لكنها أقل مرونة عندما تريد تنسيق عملها معًا. أما المكوّنات المُتحكَّم فيها فهي الأكثر مرونة، لكنها تتطلب من المكوّنات الأب ضبطها بالكامل عبر الخصائص. -In practice, "controlled" and "uncontrolled" aren't strict technical terms--each component usually has some mix of both local state and props. However, this is a useful way to talk about how components are designed and what capabilities they offer. +في الممارسة العملية، "مُتحكَّم فيه" و"غير مُتحكَّم فيه" ليسا مصطلحين تقنيين صارمين، فكل مكوّن يحمل عادةً مزيجًا من الحالة المحلية والخصائص. لكنها طريقة مفيدة للحديث عن تصميم المكوّنات والإمكانات التي تقدمها. -When writing a component, consider which information in it should be controlled (via props), and which information should be uncontrolled (via state). But you can always change your mind and refactor later. +عند كتابة مكوّن، فكّر في المعلومات التي ينبغي أن تكون مُتحكَّمًا فيها (عبر الخصائص)، والمعلومات التي ينبغي أن تكون غير مُتحكَّم فيها (عبر الحالة). ويمكنك دائمًا أن تغيّر رأيك وتعيد الهيكلة لاحقًا. -## A single source of truth for each state {/*a-single-source-of-truth-for-each-state*/} +## مصدر حقيقة واحد لكل حالة {/*a-single-source-of-truth-for-each-state*/} -In a React application, many components will have their own state. Some state may "live" close to the leaf components (components at the bottom of the tree) like inputs. Other state may "live" closer to the top of the app. For example, even client-side routing libraries are usually implemented by storing the current route in the React state, and passing it down by props! +في تطبيق React، سيكون لكثير من المكوّنات حالتها الخاصة. قد تعيش بعض الحالة قريبةً من المكوّنات الورقية (المكوّنات الموجودة في أسفل الشجرة) مثل حقول الإدخال. بينما قد تعيش حالات أخرى أقرب إلى أعلى التطبيق. فعلى سبيل المثال، تُنفَّذ مكتبات التوجيه (routing) من جهة العميل عادةً عن طريق تخزين المسار الحالي في حالة React، ثم تمريره عبر الخصائص! -**For each unique piece of state, you will choose the component that "owns" it.** This principle is also known as having a ["single source of truth".](https://en.wikipedia.org/wiki/Single_source_of_truth) It doesn't mean that all state lives in one place--but that for _each_ piece of state, there is a _specific_ component that holds that piece of information. Instead of duplicating shared state between components, *lift it up* to their common shared parent, and *pass it down* to the children that need it. +**لكل قطعة من الحالة، ستختار المكوّن الذي "يملكها".** يُعرف هذا المبدأ أيضًا باسم [«مصدر الحقيقة الوحيد» (single source of truth).](https://en.wikipedia.org/wiki/Single_source_of_truth) وهذا لا يعني أن كل الحالة يجب أن تعيش في مكان واحد، بل أن لكل قطعة من الحالة مكوّنًا *محددًا* يحمل تلك المعلومة. فبدلًا من تكرار الحالة المشتركة بين المكوّنات، *ارفعها لأعلى* إلى أقرب مكوّن أب مشترك، و*مرّرها لأسفل* إلى المكوّنات الابنة التي تحتاجها. -Your app will change as you work on it. It is common that you will move state down or back up while you're still figuring out where each piece of the state "lives". This is all part of the process! +سيتغير تطبيقك أثناء العمل عليه. ومن الشائع أن تنقل الحالة لأسفل أو لأعلى مرة أخرى أثناء محاولتك معرفة المكان الذي "تعيش" فيه كل قطعة من الحالة. وكل هذا جزء من العملية! -To see what this feels like in practice with a few more components, read [Thinking in React.](/learn/thinking-in-react) +لترى كيف يبدو ذلك عمليًا مع عدد أكبر من المكوّنات، اقرأ [التفكير في React.](/learn/thinking-in-react) -* When you want to coordinate two components, move their state to their common parent. -* Then pass the information down through props from their common parent. -* Finally, pass the event handlers down so that the children can change the parent's state. -* It's useful to consider components as "controlled" (driven by props) or "uncontrolled" (driven by state). +* عندما تريد تنسيق عمل مكوّنين، انقل حالتهما إلى أقرب مكوّن أب مشترك لهما. +* ثم مرّر المعلومات لأسفل عبر الخصائص من المكوّن الأب المشترك. +* أخيرًا، مرّر معالجات الأحداث لأسفل حتى تتمكن المكوّنات الابنة من تغيير حالة المكوّن الأب. +* من المفيد اعتبار المكوّنات "مُتحكَّمًا فيها" (مدفوعة بالخصائص) أو "غير مُتحكَّم فيها" (مدفوعة بالحالة). -#### Synced inputs {/*synced-inputs*/} +#### مدخلات متزامنة {/*synced-inputs*/} -These two inputs are independent. Make them stay in sync: editing one input should update the other input with the same text, and vice versa. +هذان الحقلان مستقلان. اجعلهما متزامنين: تعديل أحدهما يجب أن يحدّث الآخر بالنص نفسه، والعكس بالعكس. -You'll need to lift their state up into the parent component. +ستحتاج إلى رفع حالتهما لمستوى أعلى إلى المكوّن الأب. @@ -339,8 +342,8 @@ import { useState } from 'react'; export default function SyncedInputs() { return ( <> - - + + ); } @@ -366,6 +369,7 @@ function Input({ label }) { ``` ```css +body { direction: rtl; } input { margin: 5px; } label { display: block; } ``` @@ -374,7 +378,7 @@ label { display: block; } -Move the `text` state variable into the parent component along with the `handleChange` handler. Then pass them down as props to both of the `Input` components. This will keep them in sync. +انقل متغير الحالة `text` مع المعالج `handleChange` إلى المكوّن الأب، ثم مرّرهما كخصائص إلى كلا المكوّنين `Input`. بهذا سيبقى الحقلان متزامنين. @@ -391,12 +395,12 @@ export default function SyncedInputs() { return ( <> @@ -419,6 +423,7 @@ function Input({ label, value, onChange }) { ``` ```css +body { direction: rtl; } input { margin: 5px; } label { display: block; } ``` @@ -427,17 +432,17 @@ label { display: block; } -#### Filtering a list {/*filtering-a-list*/} +#### تصفية قائمة {/*filtering-a-list*/} -In this example, the `SearchBar` has its own `query` state that controls the text input. Its parent `FilterableList` component displays a `List` of items, but it doesn't take the search query into account. +في هذا المثال، يملك المكوّن `SearchBar` حالته الخاصة `query` التي تتحكم في حقل النص. أما مكوّنه الأب `FilterableList` فيعرض مكوّن `List` من العناصر، لكنه لا يأخذ استعلام البحث في الاعتبار. -Use the `filterItems(foods, query)` function to filter the list according to the search query. To test your changes, verify that typing "s" into the input filters down the list to "Sushi", "Shish kebab", and "Dim sum". +استخدم الدالة `filterItems(foods, query)` لتصفية القائمة حسب استعلام البحث. وللتحقق من تغييراتك، تأكد من أن كتابة الحرف "s" في الحقل تُصفّي القائمة إلى "Sushi" و"Shish kebab" و"Dim sum". -Note that `filterItems` is already implemented and imported so you don't need to write it yourself! +لاحظ أن `filterItems` منفّذة ومستوردة مسبقًا، لذا لا تحتاج إلى كتابتها بنفسك! -You will want to remove the `query` state and the `handleChange` handler from the `SearchBar`, and move them to the `FilterableList`. Then pass them down to `SearchBar` as `query` and `onChange` props. +ستحتاج إلى حذف حالة `query` ومعالج `handleChange` من `SearchBar`، ونقلهما إلى `FilterableList`. ثم مرّرهما إلى `SearchBar` كخاصيتين باسم `query` و`onChange`. @@ -466,7 +471,7 @@ function SearchBar() { return (