From 8f4d5414a43540f92a9a6c1ffae25cd35d207c2e Mon Sep 17 00:00:00 2001 From: jebrahimi Date: Sat, 27 Apr 2019 19:08:55 +0430 Subject: [PATCH 1/5] Translation Hooks-State.md --- content/docs/hooks-state.md | 138 ++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/content/docs/hooks-state.md b/content/docs/hooks-state.md index 052aecb33..60d7cad0f 100644 --- a/content/docs/hooks-state.md +++ b/content/docs/hooks-state.md @@ -1,20 +1,20 @@ --- id: hooks-state -title: Using the State Hook +title: استفاده از هوک state permalink: docs/hooks-state.html 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 به ری‌اکت اضافه شده‌اند. آن‌ها به شما اجازه می‌دهند که از state و دیگر قابلیت های ری‌اکت بدون نوشتن کلاس استفاده کنید. -The [previous page](/docs/hooks-intro.html) introduced Hooks with this example: +در [صفحه‌ی قبل](/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" جدید به نام state تعریف کردن یک متغیر 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: +اگر قبلا از کلاس‌ها در ری‌اکت استفاده کرده‌باشید, این کد باید برای شما آشنا باشد: ```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. +این state در شروع به عنوان `{ 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. +>شاید برای شما این سوال پیش بیاید که چرا ما استفاده از یک شمارنده(counter) را به استفاده از یک مثال واقعی‌تر ترجیح داده‌ایم. چون این به ما کمک می‌کند تا روی API تمرکز کنیم در حالی که هنوز در اول راه یادگیری هوک‌ها هستیم. -## Hooks and Function Components {#hooks-and-function-components} +## هوک‌ها و کامپوننت‌های تابعی {#hooks-and-function-components} -As a reminder, function components in React look like this: +به‌عنوان یک یادآوری, کامپوننت‌های تابعی در ری‌اکت به شکل زیر هستند: ```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". + شاید شما در گذشته این کامپوننت‌ها را با نام "کامپوننت‌های بدون state" می‌شناختید. ما حالا قابلیتی را معرفی می‌کنیم که با استفاده از آن می‌توانید از state ری‌اکت در این نوع کامپوننت‌ها استفاده کنید, بنابراین ما این نوع کامپوننت ها را "کامپوننت تابعی" می‌نامیم. -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: +مثال جدید خود را با import کردن هوک `useState` از ری‌اکت شروع می‌کنیم: ```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. +**هوک چیست؟** هوک یک تابع ویژه است که به شما اجازه می‌دهد از امکانات ری‌اکت استفاده کنید. برای مثال, هوک `useState` به شما این امکان را می‌دهد که از state در کامپوننت‌های تابعی استفاده کنید. هوک‌های دیگری هم وجود دارند که به یادگیری آن‌ها در آموزش‌های بعدی می‌پردازیم. -**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! +**چه زمانی می‌توانم از هوک استفاده کنم؟** اگر شما یک کامپوننت تابعی بنویسید و متوجه شوید که نیاز به استفاده از state در آن دارید, در گذشته و قبل از معرفی هوک‌ها مجبور بودید که کامپوننت خود را به کلاس کامپوننت تبدیل کنید. حالا با معرفی هوک‌ها می‌توانید هر وقت که بخواهید در کامپوننت‌های تابعی خود از state استفاده کنید. ما در ادامه این کار را انجام خواهیم داد! ->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} +## تعریف کردین متغیر state {#declaring-a-state-variable} -In a class, we initialize the `count` state to `0` by setting `this.state` to `{ count: 0 }` in the constructor: +در این کلاس, ما مقدار اولیه‌ی state `count` را با مساوی قرار دادن مقدار `this.state` با `{ count: 0 }` در constructor برابر `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" جدید به نام state تعریف کردن یک متغیر 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" است. ما در اینجا متغیری به نام `count` داریم ولی می‌توانیم نام دلخواه, مثل `banana` هم برای آن انتخاب کنیم. این یک راه برای "حفظ" برخی مقادیر میان تماس‌های تابعی است — `useState` یک راه جدید است که دقیقا امکانات مشابه `this.state` در کلاس را فراهم می‌کند. به طور معمول, مقادیر موجود در تابع پس از بسته شدن تابع از بین می‌روند ولی متغیر state پس از بسته شدن تابع هم توسط ری‌اکت حفظ می‌شود. -**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()` می‌دهیم مقدار اولیه‌ی state است. بر خلاف کاری که در کلاس ها انجام می‌دهیم, state حتما نباید یک object باشد. اگر ما به Number یا String احتیاج داریم می توانیم state را به عنوان Number یا String تعریف کنیم. در مثال, ما فقط به یک عدد احتیاج داریم که بفهمیم کاربر چند بار کلیک کرده‌است, پس `0` را به عنوان مقدار اولیه‌ی state خود به useState پاس‌ ‌می‌دهیم. (اگر ما بخواهیم دو مقدار متفاوت را در state ذخیره کنیم, باید دوبار از `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` چه چیزی را بر می‌گرداند؟** این هوک یک جفت مقدار را بر می‌گرداند: یک state به نام count و یک تابع برای بروزرسانی مقدار آن. به این دلیل است که ما آن را به صورت `const [count, setCount] = useState()` نوشته‌ایم. این مقادیر مشابه `this.state.count` و `this.setState` در کلاس کامپوننت هستند, اگر شما با این نوع syntax آشنایی ندارید, مادر [پایین این صفحه](/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" جدید به نام state تعریف کردن یک متغیر 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`. +ما یک متغیر state به نام `count` تعریف کرده‌ایم, و مقدار آن را برابر `0` قرار داده‌ایم. ری‌اکت مقدار این state را میان رندر کردن دوباره‌ی کامپوننت حفظ می‌کند, و جدیدترین مقدار آن را برای تابع فراهم می‌کند. اگر ما بخواهم مقدار `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" بسیار دقیق نخواهد بود چون یک state فقط در زمان اولین رندر یک کامپوننت ساخته می‌شود. در هنگام رندر های بعدی, `useState` فقط مقدار state را به ما می‌دهد. در غیر این صورت اگر قرار بود در هر رندر یک state با همان مقدار ساخته شود که دیگر state نبودند! یک دلیل دیگر هم وجود دارد *همیشه* نام هوک‌ها با `use` شروع می‌شود.دلیل این را هم می‌توانید در [قوانین هوک‌ها](/docs/hooks-rules.html) مطالعه کنید. -## Reading State {#reading-state} +## خواندن 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} +## بروزرسانی State {#updating-state} -In a class, we need to call `this.setState()` to update the `count` state: +در کلاس, ما باید با استفاده از `this.setState()` مقدار state `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. +حالا بیاید **خلاصه چیزهایی را که یادگرفته‌ایم را خط به خط برسی کنیم** و بفهمیم چه چیزهایی را یاد گرفته‌ایم.