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/importing-and-exporting-components.md
+54-54Lines changed: 54 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -4,23 +4,23 @@ title: Importing and Exporting Components
4
4
5
5
<Intro>
6
6
7
-
The magic of components lies in their reusability: you can create components that are composed of other components. But as you nest more and more components, it often makes sense to start splitting them into different files. This lets you keep your files easy to scan and reuse components in more places.
7
+
کاربرد اصلی کامپوننت ها, قابلیت استفاده مجدد آنهاست: شما میتوانید کامپوننت هایی بسازید که متشکل از کامپوننت های دیگر هستند ولی درگل بهتر است که هربخشی را تبدیل به کامپوننت کنید چون اسکن کردن آنها توسط اجرا کننده آسان تر است.
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
13
-
*What a root component file is
14
-
*How to import and export a component
15
-
*When to use default and named imports and exports
16
-
*How to import and export multiple components from one file
17
-
*How to split components into multiple files
13
+
*کامپوننت ریشه (root) چیست
14
+
*چگونه کامپوننتی را ایمپورت یا اکسپورت (import - export) کنیم
15
+
*از دستور default استفاده کنم
16
+
*چگونه چندین کامپوننت که داخل یک فایل هستند را export کنم.
17
+
*چگونه در چندین فایل کامپوننت هارا تقسیم کنم
18
18
19
19
</YouWillLearn>
20
20
21
-
## The root component file {/*the-root-component-file*/}
21
+
## فایل کامپوننت اصلی {/*the-root-component-file*/}
22
22
23
-
In [Your First Component](/learn/your-first-component), you made a `Profile`component and a `Gallery`component that renders it:
23
+
در بخش [Your First Component](/learn/your-first-component), شما یک `Profile`کامپوننت ساختید و یک کامپوننت `Gallery`داخل آن رندر میشود:
These currently live in a **root component file,** named `App.js`in this example. Depending on your setup, your root component could be in another file, though. If you use a framework with file-based routing, such as Next.js, your root component will be different for every page.
55
+
در این مثال, کامپوننت ریشه فایل `App.js`است. ممکن است این فایل در پروژه های مختلفی مانند NextJS متفاوت است
56
56
57
-
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
57
+
## ایمپورت و اکسپورت کردن کامپوننت ها {/*exporting-and-importing-a-component*/}
58
58
59
-
What if you want to change the landing screen in the future and put a list of science books there? Or place all the profiles somewhere else? It makes sense to move `Gallery`and`Profile`out of the root component file. This will make them more modular and reusable in other files. You can move a component in three steps:
59
+
به عنوان مثال فرض کنید در یک لیست چند کامپوننت `Gallery`یا`Profile`داشته باشید. بهتر است داخل فایل کامپوننت اصلی نباشند و در کامپوننت های جداگانه ای قرار بدهید.
60
60
61
-
1.**Make**a new JS file to put the components in.
62
-
2.**Export** your function component from that file (using either[default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export)or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports)exports).
63
-
3.**Import** it in the file where you’ll use the component (using the corresponding technique for importing [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults)or[named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) exports).
61
+
1.یک فایل جاوااسکریپتی **بسازید**و تبدیل به یک کامپوننت کنید
62
+
2.در آن فایل, کامپوننت های خود را اکسپورت کنید (از [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export)یا [name export](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports)کنید).
63
+
3.همان کامپوننت را در هر فایلی که میخواهید از آن استفاده کنید ایمپورت کنید (استفاده از تکنیک ایمپورت [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults)یا[named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) exports).
64
64
65
-
Here both `Profile`and`Gallery`have been moved out of `App.js`into a new file called `Gallery.js`. Now you can change `App.js` to import `Gallery`from `Gallery.js`:
65
+
خب, اینجا کامپوننت های `Profile`و`Gallery`را ساخته ایم و داخل فایل `App.js`ایمپورت کردیم و داریم از آنها استفاده کنیم. حال در دستور ایمپورت مربوط به کامپوننت `Gallery`, بجای `Gallery`نام آن را به `Gallery.js` تغییر دهید
Notice how this example is broken down into two component files now:
107
+
به موارد زیر که به دو کامپوننت تقسیم شدند دقت کنید:
108
108
109
109
1.`Gallery.js`:
110
-
-Defines the `Profile`component which is only used within the same file and is not exported.
111
-
-Exports the`Gallery`component as a **default export.**
110
+
-کامپوننت `Profile`را صرفا تعریف کرده و آن را اکسپورت نکرده
111
+
-از کامپوننت`Gallery`به عنوان **default export.** اکسپورت گرفته
112
112
2.`App.js`:
113
-
-Imports`Gallery`as a **default import**from`Gallery.js`.
114
-
-Exports the root`App`component as a**default export.**
113
+
-کامپوننت`Gallery`را به عنوان **default import**از`Gallery.js` ایمپورت کرده
114
+
-کامپوننت اصلی یا`App`را به عنوان**default export.** اکسپورت کرده
115
115
116
116
117
117
<Note>
118
118
119
-
You may encounter files that leave off the `.js`file extension like so:
119
+
ممکن است با فایل هایی مواجه بشوید که پسوند `.js`را نداشته باشند:
120
120
121
121
```js
122
122
importGalleryfrom'./Gallery';
123
123
```
124
124
125
-
Either `'./Gallery.js'`or`'./Gallery'`will work with React, though the former is closer to how [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules)work.
125
+
دو آدرس `'./Gallery.js'`یا`'./Gallery'`داخل ریکت کار میکنند, اگر چه مورد اول به سازوکار [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules)نزدیک تر است.
126
126
127
127
</Note>
128
128
129
129
<DeepDive>
130
130
131
-
#### Default vs named exports {/*default-vs-named-exports*/}
131
+
#### دستور default و export {/*default-vs-named-exports*/}
132
132
133
-
There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. **A file can have no more than one _default_export, but it can have as many _named_ exports as you like.**
133
+
در اینجا دو راه اصلی وجود دارد که کامپوننت ها یا توابع و یا متغیر هایمان را export کنیم: default export و export خالی. تا اینجا در مثال ها از default export استفاده شده اما از این به بعد شما میتوانید از دستور export خالی هم استفاده کنید.
134
134
135
135

136
136
137
-
How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:
137
+
اینکه چطور کامپوننت را ایمپورت کنید بستگی دارد به اینکه چطور آن را اکسپورت کنید. و اگر در جای خود استفاده نکنید به ارور برمیخورید.
138
138
139
139
| Syntax | Export statement | Import statement |
140
140
| ----------- | ----------- | ----------- |
141
141
| Default |`export default function Button() {}`|`import Button from './Button.js';`|
142
142
| Named |`export function Button() {}`|`import { Button } from './Button.js';`|
143
143
144
-
When you write a _default_import, you can put any name you want after `import`. For example, you could write `import Banana from './Button.js'`instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That's why they are called _named_ imports!
144
+
زمانی که شما به صورت پیش فرض ایمپورت میکنید (default import), شما میتوانید هر اسمی را بعد از import به عنوان نام متغیر بنویسید, حتی میتوانید مثلا بنویسید `import Banana from './Button.js'`ولی در ایمپورت هایی دیگر باید اسمی که در همان فایل اکسپورت کردید را بنویسید.
145
145
146
-
**People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values.**Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like `export default () => {}`, are discouraged because they make debugging harder.
146
+
**معمولا از ایمپورت پیش فرض استفاده میشود, مخصوصا زمانی که یک کامپوننت یا تابع داخل یک فایل داریم. و زمانی که از ایمپورت معمولی (named import) که در یک فایل چندین تابع یا کامپوننت داشته باشیم**صرف نظر از اینکه از کدام روش استفاده میکنید, سعی کنید نام های مناسبی برای توابع و کامپوننت ها و ایمپورت ها انتخاب کنید. دستوری مانند `export default () => {}` مشکل زا هست و حذف میشود.
147
147
148
148
</DeepDive>
149
149
150
-
## Exporting and importing multiple components from the same file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
150
+
## اکسپورت و ایمپورت کردن چند کامپوننت از یک فایل {/*exporting-and-importing-multiple-components-from-the-same-file*/}
151
151
152
-
What if you want to show just one`Profile`instead of a gallery? You can export the `Profile` component, too. But `Gallery.js` already has a *default* export, and you can't have _two_ default exports. You could create a new file with a default export, or you could add a *named*export for `Profile`. **A file can only have one default export, but it can have numerous named exports!**
152
+
برگردیم به مثال قبلی, فرض کنیم دو کامپوننت`Profile`و `Gallery` را داخل یک فایل داشته باشیم. نمیتوانیم به صورت پیش فرض از آنها اکسپورت بگیریم (export default). پس چکار کنیم؟ از اکسپورت معمولی (export) استفاده میکنیم. **در یک فایل فقط یک export default داشته باشد ولی به تعداد مدنظر میتوانید اکسپورت معمولی داشته باشید**
153
153
154
154
<Note>
155
155
156
-
To reduce the potential confusion between default and named exports, some teams choose to only stick to one style (default or named), or avoid mixing them in a single file. Do what works best for you!
156
+
شاید پیش خود سوال کنید که از کدام استفاده کنم؟ یکسری از تیم ها فقط از export default استفاده کنند و مابقی توابع داخل فایل را با آن ادغام کنند. یا از export سراسر فایل استفاده کنند. اینکه کدام بهتر است به خودتان بستگی دارد.
157
157
158
158
</Note>
159
159
160
-
First, **export**`Profile`from `Gallery.js`using a named export (no `default`keyword):
160
+
ابتدا, کامپوننت`Profile`را که در فایل `Gallery.js`موجود است را به اکسپورت بگیرید (از کلمه کلیدی `default`استفاده نکنید):
161
161
162
162
```js
163
163
exportfunctionProfile() {
164
164
// ...
165
165
}
166
166
```
167
167
168
-
Then, **import**`Profile`from `Gallery.js`to `App.js`using a named import (with the curly braces):
168
+
سپس, کامپوننت`Profile`را از فایل `Gallery.js`به فایل `App.js`ایمپورت کنید (از علامت آکولاد استفاده کنید):
و در آخر, کامپوننت`<Profile />`در کامپوننت`App`رندر میشود:
175
175
176
176
```js
177
177
exportdefaultfunctionApp() {
178
178
return<Profile />;
179
179
}
180
180
```
181
181
182
-
Now `Gallery.js`contains two exports: a default `Gallery` export, and a named `Profile` export. `App.js`imports both of them. Try editing`<Profile />`to `<Gallery />`and back in this example:
182
+
حال در اینجا فایل `Gallery.js`دو export دارد: یک default export مربوط به کامپوننت `Gallery`, و یک named export مربوط به کامپوننت `Profile`. در فایل `App.js`هردوی آنها ایمپورت میشوند. حال دستور`<Profile />`را به `<Gallery />`تغییر بدهید و به مثال بازگردید:
Now you're using a mix of default and named exports:
225
+
حال شما از هر دو نوع از دستور export استفاده کردید:
226
226
227
227
*`Gallery.js`:
228
-
-Exports the `Profile`component as a**named export called `Profile`.**
229
-
-Exports the `Gallery`component as a**default export.**
228
+
-کامپوننت `Profile`به عنوان **named export** که نام آن `Profile` است اکسپورت شده.
229
+
-کامپوننت `Gallery`به عنوان **default export** اکسپورت شده است
230
230
*`App.js`:
231
-
-Imports`Profile`as a **named import called `Profile`** from `Gallery.js`.
232
-
-Imports`Gallery`as a **default import**from `Gallery.js`.
233
-
-Exports the root `App`component as a **default export.**
231
+
-کامپوننت`Profile`به عنوان **named import** که نام آن `Profile` است در `Gallery.js` ایمپورت شده.
232
+
-کامپوننت`Gallery`به عنوان **default import**در فایل `Gallery.js` ایمپورت شده.
233
+
-کامپوننت `App`که کامپوننت ریشه است به عنوان **default export** اکسپورت شده است.
234
234
235
235
<Recap>
236
236
237
-
On this page you learned:
237
+
مواردی که در این صفحه یاد گرفتید:
238
238
239
-
*What a root component file is
240
-
*How to import and export a component
241
-
*When and how to use default and named imports and exports
242
-
*How to export multiple components from the same file
239
+
*کامپوننت اصلی (root) چیست
240
+
*چگونه یک کامپوننت را export/import کنیم
241
+
*چه زمانی از export/import پیش فرض یا معمولی استفاده کنیم
242
+
*چگونه چندین کامپوننت را که داخل یک فایل هستند را اکسپورت کنیم
243
243
244
244
</Recap>
245
245
246
246
247
247
248
248
<Challenges>
249
249
250
-
#### Split the components further {/*split-the-components-further*/}
250
+
#### تا جایی که میتوانید کامپوننت هارا به کامپوننت های کوچک تری تقسیم کنید {/*split-the-components-further*/}
251
251
252
-
Currently, `Gallery.js`exports both`Profile`and`Gallery`, which is a bit confusing.
252
+
در حال حاضر, فایل `Gallery.js`دو کامپوننت`Profile`و`Gallery` را اکسپورت میکند, که یک مقداری گیج کنندست.
253
253
254
-
Move the `Profile` component to its own `Profile.js`, and then change the `App` component to render both `<Profile />` and `<Gallery />` one after another.
254
+
یک فایلی را با نام `Profile.js` بسازید و کامپوننت `Profile` را درون آن بیاورید و اکسپورت کنید. سپس در کامپوننت `App` همان را ایمپورت کنید.
255
255
256
-
You may use either a default or a named export for `Profile`, but make sure that you use the corresponding import syntax in both `App.js` and `Gallery.js`! You can refer to the table from the deep dive above:
256
+
هنگام استفاده از دستور export و import مطمئن شوید از کدام روش استفاده میکنید. برای این منظور جدول زیر را چک کنید:
257
257
258
258
| Syntax | Export statement | Import statement |
259
259
| ----------- | ----------- | ----------- |
@@ -262,7 +262,7 @@ You may use either a default or a named export for `Profile`, but make sure that
262
262
263
263
<Hint>
264
264
265
-
Don't forget to import your components where they are called. Doesn't `Gallery` use `Profile`, too?
265
+
زمانی که کامپوننت هارا صدا میزنید, حتما کامپوننت هارا import کنید. به نظر شما تا اینجا ما کامپوننت `Profile` را هم در فایل `Gallery.js` داریم؟
266
266
267
267
</Hint>
268
268
@@ -282,7 +282,7 @@ export default function App() {
0 commit comments