diff --git a/content/docs/hooks-state.md b/content/docs/hooks-state.md index 0b4d15b65..82485aa77 100644 --- a/content/docs/hooks-state.md +++ b/content/docs/hooks-state.md @@ -6,15 +6,15 @@ next: hooks-effect.html prev: hooks-overview.html --- -*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class. +*الخطافات* هي إضافة جديدة إلى الإصدار 16.8 في React، إذ تسمح لك باستعمال ميزة الحالة وميزات React الأخرى دون كتابة أي صنف. -The [introduction page](/docs/hooks-intro.html) used this example to get familiar with Hooks: +شرحت الصفحة السابقة في قسم [خطاف الحالة](/docs/hooks-intro.html) هذا الخطاف عبر المثال التالي: ```js{4-5} import React, { useState } from 'react'; function Example() { - // Declare a new state variable, which we'll call "count" + // "count" التصريح عن متغير حالة جديد ندعوه const [count, setCount] = useState(0); return ( @@ -28,11 +28,11 @@ function Example() { } ``` -We'll start learning about Hooks by comparing this code to an equivalent class example. +بادئ ذي بدء، سنتعرف على الخطافات عبر موازنة هذه الشيفرة مع الصنف المكافئ لها. -## Equivalent Class Example {#equivalent-class-example} +## مثال عن صنف مكافئ لخطاف {#equivalent-class-example} -If you used classes in React before, this code should look familiar: +إن استعملت الأصناف في React من قبل، فيجب أن تكون الشيفرة التالية مألوفةً لديك: ```js class Example extends React.Component { @@ -56,39 +56,39 @@ class Example extends React.Component { } ``` -The state starts as `{ count: 0 }`, and we increment `state.count` when the user clicks a button by calling `this.setState()`. We'll use snippets from this class throughout the page. +الحالة الأولية تكون `{ count: 0 }`, ونعمل على زيادة `state.count` عندما يضغط المستخدم على زر يستدعي `this.setState()`. سنستعمل أجزاء من هذا الصنف في أقسام لاحقة من هذه الصفحة. ->Note +>ملاحظة > ->You might be wondering why we're using a counter here instead of a more realistic example. This is to help us focus on the API while we're still making our first steps with Hooks. +>قد تتساءل عن سبب استعمالنا مثال عدَّادٍ هنا عوضَ استعمال مثال أكثر واقعية. يكمن السبب في أنَّ هذا المثال البسيط يساعدنا على التركيز على الواجهة البرمجية في بداية رحلتنا مع الخطافات. -## Hooks and Function Components {#hooks-and-function-components} +## الخطافات ومكونات دالة {#hooks-and-function-components} -As a reminder, function components in React look like this: +لنتذكر سويةً، تبدو مكونات دالة في React بالشكل: ```js const Example = (props) => { - // You can use Hooks here! + // تستطيع استعمال الخطافات هنا return
; } ``` -or this: +أو الشكل التالي: ```js function Example(props) { - // You can use Hooks here! + // تستطيع استعمال الخطافات هنا return
; } ``` -You might have previously known these as "stateless components". We're now introducing the ability to use React state from these, so we prefer the name "function components". +ربما كنت تعرف مسبقًا أنَّ هذه المكونات هي "مكونات عديمة الحالة" (stateless components). نعرِّف الآن إمكانية استعمال حالة React من هذه المكونات، لذا نفضل استعمال الاسم "مكونات دالة". -Hooks **don't** work inside classes. But you can use them instead of writing classes. +**لا تعمل**الخطافات داخل الأصناف، ولكن يمكن استعمالها بدلًا من الأصناف نفسها. -## What's a Hook? {#whats-a-hook} +## ما هو الخطاف؟ {#whats-a-hook} -Our new example starts by importing the `useState` Hook from React: +يبدأ مثالنا الجديد باستيراد الخطاف `useState` من React: ```js{1} import React, { useState } from 'react'; @@ -98,17 +98,17 @@ function Example() { } ``` -**What is a Hook?** A Hook is a special function that lets you "hook into" React features. For example, `useState` is a Hook that lets you add React state to function components. We'll learn other Hooks later. +**إذًا، ما هو الخطاف تحديدًا؟** الخطاف هو دالة مميزة تمكنك من "تعليق أو ربط" (hook into) ميزات React مع بعضها. على سبيل المثال، `useState` هو خطاف يمكنك من إضافة حالة React إلى مكونات دالة. سنتطرق إلى الخطافات الأخرى لاحقًا. -**When would I use a Hook?** If you write a function component and realize you need to add some state to it, previously you had to convert it to a class. Now you can use a Hook inside the existing function component. We're going to do that right now! +**متى يمكنني استعمال الخطاف؟** إن كتبت مكون دالة ووجدت أنَّك بحاجة إلى إضافة بعض الحالة له، فستحتاج - سابقًا قبل عصر الخطافات - إلى تحويله إلى صنف. الآن، يمكنك استعمال خطاف داخل مكون دالة موجودة وهو ما سنفعله الآن. ->Note: +>ملاحظة: > ->There are some special rules about where you can and can't use Hooks within a component. We'll learn them in [Rules of Hooks](/docs/hooks-rules.html). +>هنالك بعض القواعد المخصَّصة تحدِّد المكان المسموح والمحظور فيه استعمال الخطافات ضمن مكون. سنشرح هذه القواعد بالتفصيل الممل في توثيق [قواعد استعمال الخطافات](/docs/hooks-rules.html). -## Declaring a State Variable {#declaring-a-state-variable} +## التصريح عن متغير حالة {#declaring-a-state-variable} -In a class, we initialize the `count` state to `0` by setting `this.state` to `{ count: 0 }` in the constructor: +إن كنا سنستعمل صنفًا، نهيِّئ فيه الحالة `count` إلى 0 عبر ضبط `this.state` إلى `{ count: 0 }` في باني هذا الصنف: ```js{4-6} class Example extends React.Component { @@ -120,58 +120,58 @@ class Example extends React.Component { } ``` -In a function component, we have no `this`, so we can't assign or read `this.state`. Instead, we call the `useState` Hook directly inside our component: +في مكون دالة، لا يمكننا استعمال `this`, لذا لا نستطيع إسناد أو قراء `this.state`. عوض ذلك، يمكننا استدعاء الخطاف `useState` مباشرةً داخل المكون الخاص بنا: ```js{4,5} import React, { useState } from 'react'; function Example() { - // Declare a new state variable, which we'll call "count" + // "count" التصريح عن متغير حالة جديد ندعوه const [count, setCount] = useState(0); ``` -**What does calling `useState` do?** It declares a "state variable". Our variable is called `count` but we could call it anything else, like `banana`. This is a way to "preserve" some values between the function calls — `useState` is a new way to use the exact same capabilities that `this.state` provides in a class. Normally, variables "disappear" when the function exits but state variables are preserved by React. +**ماذا يفعل استدعاء `useState` ؟** إنَّه يصرِّح عن "متغير حالة" (state variable). هذا المتغير يدعى `count` ولكن يمكننا أن ندعوه بأي اسم آخر مثل`banana`. هذه هي طريقة لحفظ بعض القيم بين استدعاءات الدالة، إذ `useState` هي طريقة جديدة لاستعمال الامكانيات نفسها التي توفرها `this.state` في الصنف. عمومًا، المتغيرات "تختفي" عند اكتمال تنفيذ الدالة وخروجها ولكن متغيرات الحالة تحافظ على قيمتها في React. -**What do we pass to `useState` as an argument?** The only argument to the `useState()` Hook is the initial state. Unlike with classes, the state doesn't have to be an object. We can keep a number or a string if that's all we need. In our example, we just want a number for how many times the user clicked, so pass `0` as initial state for our variable. (If we wanted to store two different values in state, we would call `useState()` twice.) +**لماذا نمرر وسيطًا إلى useState؟** الوسيط الوحيد الذي يمكن تمريره إلى الخطاف `useState()` هو الحالة الأولية. خلافًا للأصناف، ليس من الضروري أن تكون الحالة كائنًا. يمكننا استعمال عدد أو سلسلة نصية إن كان ذلك ما نحتاجه. في مثالنا، نحتاج إلى عدد ليمثل عدد المرات التي ضغط فيها المستخدم على الزر، لذا مرَّرنا العدد 0 ليكون الحالة الأولية لمتغيرنا. (إن أردنا تخزين قيمتين مختلفتين في حالة، فيمكننا فعل ذلك عبر استدعاء `useState()` مرتين.) -**What does `useState` return?** It returns a pair of values: the current state and a function that updates it. This is why we write `const [count, setCount] = useState()`. This is similar to `this.state.count` and `this.setState` in a class, except you get them in a pair. If you're not familiar with the syntax we used, we'll come back to it [at the bottom of this page](/docs/hooks-state.html#tip-what-do-square-brackets-mean). +**ما الذي يعيده`useState`؟** إنه يعيد زوجًا من القيم: الحالة الحالية، ودالة تحدِّثها. لهذا السبب، نكتب `const [count, setCount] = useState()`. هذا الأمر يشبه `this.state.count` و `this.setState` في الصنف، باستثناء أنَّنا نحصل عليهم كزوج من القيم. إن لم تكن الصيغة السابقة التي استعملناها مألوفة لك، فسنتطرق إليها في [آخر هذه الصفحة](/docs/hooks-state.html#tip-what-do-square-brackets-mean). -Now that we know what the `useState` Hook does, our example should make more sense: +الآن وقد عرفنا ما الذي يفعله الخطاف `useState` يجب أن تكون الشيفرة التالية مفهومة تمامًا لك: ```js{4,5} import React, { useState } from 'react'; function Example() { - // Declare a new state variable, which we'll call "count" + // "count" التصريح عن متغير حالة جديد ندعوه const [count, setCount] = useState(0); ``` -We declare a state variable called `count`, and set it to `0`. React will remember its current value between re-renders, and provide the most recent one to our function. If we want to update the current `count`, we can call `setCount`. +صرَّحنا عن متغير حالة يدعى `count`, وأسندنا القيمة 0 العددية له. ستتذكر React قيمته الحالية بين عمليات إعادة التصيير، وتوفر الحالة الأحدث له للدالة. إن أردنا تحديث قيمة المتغير `count` الحالية، يمكننا استدعاء `setCount`. ->Note +>ملاحظة > ->You might be wondering: why is `useState` not named `createState` instead? +>قد تتسائل عن سبب استعمال الاسم `useState` لهذا الخطاف وليس الاسم `createState`? > ->"Create" wouldn't be quite accurate because the state is only created the first time our component renders. During the next renders, `useState` gives us the current state. Otherwise it wouldn't be "state" at all! There's also a reason why Hook names *always* start with `use`. We'll learn why later in the [Rules of Hooks](/docs/hooks-rules.html). +>الجواب هو أنَّ "إنشاء" (create) لن تكون دقيقة والسبب أنَّ الحالة تُنشَأ عندما يصيَّر المكون أول مرة فقط. في عمليات التصيير اللاحقة، يعطينا الخطاف `useState` القيمة (الحالة) الحالية وإلا لما كنا أطلقنا عليها "حالة" (state) على الإطلاق. هنالك أيضًا سبب متعلق ببدء تسمية الخطافات بالكلمة `use` ، وسنتعرف عليه في توثيق [قواعد استعمال الخطافات](/docs/hooks-rules.html). -## Reading State {#reading-state} +## قراءة الحالة {#reading-state} -When we want to display the current count in a class, we read `this.state.count`: +إن أردنا إظهار قيمة المتغير count الحالية في صنف، يمكن أن نقرأ `this.state.count`: ```js

You clicked {this.state.count} times

``` -In a function, we can use `count` directly: +في دال، يمكننا استعمال المتغير `count` مباشرةً: ```js

You clicked {count} times

``` -## Updating State {#updating-state} +## تحديث الحالة {#updating-state} -In a class, we need to call `this.setState()` to update the `count` state: +في الصنف، نحتاج إلى استدعاء `this.setState()` لتحديث الحالة `count`: ```js{1} ``` -In a function, we already have `setCount` and `count` as variables so we don't need `this`: +في الدالة، لدينا `setCount` و `count` مسبقًا كمتغيرات، لذا لا تحتاج إلى `this`: ```js{1} ``` -## Recap {#recap} +## الخلاصة {#recap} -Let's now **recap what we learned line by line** and check our understanding. +**لنراجع ما تعلمناه بالمرور على كل سطر من أسطر الشيفرة** والتأكد من فهمنا للخطافات بشكل عام ولخطاف الحالة بشكل خاص: