diff --git a/content/docs/code-splitting.md b/content/docs/code-splitting.md index 9d1101cdc..be6c9cfe2 100644 --- a/content/docs/code-splitting.md +++ b/content/docs/code-splitting.md @@ -1,19 +1,16 @@ --- id: code-splitting -title: Code-Splitting +title: تکه تکه کردن کد permalink: docs/code-splitting.html --- -## Bundling {#bundling} +## بسته بندی کردن (bundling) {#bundling} -Most React apps will have their files "bundled" using tools like -[Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/) or -[Browserify](http://browserify.org/). -Bundling is the process of following imported files and merging them into a -single file: a "bundle". This bundle can then be included on a webpage to load -an entire app at once. +در بیشتر برنامه های ری‌اکت فایل ها توسط ابزار هایی مثل [Webpack](https://webpack.js.org/) یا [Browserify](http://browserify.org/) بسته بندی می شوند -#### Example {#example} +پروسه ی bundle کردن به این شکل است که فایل های import شده دنبال می شود و همه ی آنها در یک فایل با نام "bundle" ادغام می شوند. این فایل bundle می تواند در صفحه ی وب بارگزاری شود تا کل برنامه را به یکباره اجرا کند. + +#### مثال {#example} **App:** @@ -41,44 +38,34 @@ function add(a, b) { console.log(add(16, 26)); // 42 ``` -> Note: +> نکته: > -> Your bundles will end up looking a lot different than this. +> bundle های شما در نهایت با این نمونه تفاوت زیادی خواهد داشت. + +اگر شما از [Gatsby](https://www.gatsbyjs.org/) ،[Next.js](https://github.com/zeit/next.js/) ،[Create React App](https://github.com/facebookincubator/create-react-app) یا ابزار های مشابه استفاده می کنید، بطور پیشفرض Webpack تنظیم شده است تا برنامه ی شما را بسته بندی (bundle) کند. -If you're using [Create React App](https://github.com/facebookincubator/create-react-app), [Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/), or a similar tool, you will have a Webpack setup out of the box to bundle your -app. +در غیر این صورت، لازم است که پروسه ی بسته بندی را خودتان تنظیم و راه اندازی کنید. برای نمونه راهنمایی های [نصب](https://webpack.js.org/guides/installation/) و +[شروع به کار](https://webpack.js.org/guides/getting-started/) را از مستندات Webpack مشاهده کنید. -If you aren't, you'll need to setup bundling yourself. For example, see the -[Installation](https://webpack.js.org/guides/installation/) and -[Getting Started](https://webpack.js.org/guides/getting-started/) guides on the -Webpack docs. +## تکه تکه کردن کد {#code-splitting} -## Code Splitting {#code-splitting} +بسته بندی بسیار خوب است، اما همواره با رشد و بزرگ شدن برنامه تان، فایل bundle شما نیز بزرگ می شود. به ویژه اگر شما از کتابخانه های جانبی استفاده کنید. +لازم است که همیشه چشمتان به کد هایی باشد که به فایل bundle تان اضافه می کنید در این صورت فایل فوق بصورت تصادفی بسیار بزرگ نمی شود که باعث شود برنامه ی شما زمان زیادی برای لود شدن بگیرد. -Bundling is great, but as your app grows, your bundle will grow too. Especially -if you are including large third-party libraries. You need to keep an eye on -the code you are including in your bundle so that you don't accidentally make -it so large that your app takes a long time to load. +برای جلوگیری از درگیر شدن با مشکلات bundle بزرگ، بهتر است که قبل از ایجاد مشکل شروع به "تکه تکه" کردن فایل bundle تان کنید. + [تکه تکه کردن کد](https://webpack.js.org/guides/code-splitting/) امکانی است که توسط کتابخانه های بسته بندی کننده مثل Webpack و Browserify (توسط +[factor-bundle](https://github.com/browserify/factor-bundle)) پشتیبانی می شود به این صورت که می توانند چندین فایل bundle ایجاد کنند که هنگام اجرای برنامه بصورت پویا لود شوند. + +تکه تکه کردن برنامه تان به شما کمک می کند که تنها چیز هایی که در حال حاضر کاربر نیاز دارد را به روش "lazy-load" بارگیری کنید که به صورت رویایی کارایی برنامه ی شما را افزایش می دهد. در این صورت در حالی که شما مقدار کلی کد خود را در برنامه کاهش نداده اید، از بارگیری کدی که ممکن است کاربر اصلا به آن نیاز نداشته باشد جلوگیری می کنید و مقدار کد مورد نیاز در بارگیری اولیه را کاهش می دهید. -To avoid winding up with a large bundle, it's good to get ahead of the problem -and start "splitting" your bundle. -Code-Splitting is a feature -supported by bundlers like [Webpack](https://webpack.js.org/guides/code-splitting/), [Rollup](https://rollupjs.org/guide/en/#code-splitting) and Browserify (via -[factor-bundle](https://github.com/browserify/factor-bundle)) which can create -multiple bundles that can be dynamically loaded at runtime. -Code-splitting your app can help you "lazy-load" just the things that are -currently needed by the user, which can dramatically improve the performance of -your app. While you haven't reduced the overall amount of code in your app, -you've avoided loading code that the user may never need, and reduced the amount -of code needed during the initial load. ## `import()` {#import} -The best way to introduce code-splitting into your app is through the dynamic -`import()` syntax. +بهترین راه برای شروع استفاده از تکه تکه کردن کد در برنامه تان استفاده از سینتکس `import()` پویا است. + -**Before:** +**قبل:** ```js import { add } from './math'; @@ -86,7 +73,7 @@ import { add } from './math'; console.log(add(16, 26)); ``` -**After:** +**بعد:** ```js import("./math").then(math => { @@ -94,49 +81,45 @@ import("./math").then(math => { }); ``` -> Note: +> نکته: > -> The dynamic `import()` syntax is a ECMAScript (JavaScript) -> [proposal](https://github.com/tc39/proposal-dynamic-import) not currently -> part of the language standard. It is expected to be accepted in the -> near future. +> سینتکس `import()` پویا، یک [پیشنهاد](https://github.com/tc39/proposal-dynamic-import) اکما اسکریپت (جاوا اسکریت) است که در حال حاضر بخشی از استاندارد زبان نیست. ولی انتظار می رود که در آینده ی نزدیک پذیرفته شود. -When Webpack comes across this syntax, it automatically starts code-splitting -your app. If you're using Create React App, this is already configured for you -and you can [start using it](https://facebook.github.io/create-react-app/docs/code-splitting) immediately. It's also supported -out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import). +هنگامی که Webpack با این سینکتس برخورد می کند، بصورت خودکار شروع به تکه تکه کردن کد برنامه ی شما می کند. اگر شما از Create React App استفاده می کنید، این در حال حاضر برای شما پیکربندی شده است و شما می توانید همین حالا [از اینجا شروع به استفاده از آن کنید](https://facebook.github.io/create-react-app/docs/code-splitting). همچنین بصورت پیشفرض توسط [Next.js](https://github.com/zeit/next.js/#dynamic-import) نیز پشتیبانی می شود. -If you're setting up Webpack yourself, you'll probably want to read Webpack's -[guide on code splitting](https://webpack.js.org/guides/code-splitting/). Your Webpack config should look vaguely [like this](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269). +اگر شما Webpack را خودتان پیکربندی و راه اندازی کرده اید، ممکن است بخواهید [راهنمای تکه تکه کردن کد Webpack](https://webpack.js.org/guides/code-splitting/) را بخوانید. پیکربندی Webpack شما احتمالا چیزی [شبیه به این](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269) باشد. + +هنگام استفاده از [Babel](https://babeljs.io/)، شما باید اطمینان حاصل کنید که بابل می تواند سینتکس import پویا را parse کند ولی آن را تغییر ندهد. در این راستا شما به [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import) نیاز خواهید داشت. -When using [Babel](https://babeljs.io/), you'll need to make sure that Babel can -parse the dynamic import syntax but is not transforming it. For that you will need [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import). ## `React.lazy` {#reactlazy} -> Note: +> نکته: > -> `React.lazy` and Suspense are not yet available for server-side rendering. If you want to do code-splitting in a server rendered app, we recommend [Loadable Components](https://github.com/smooth-code/loadable-components). It has a nice [guide for bundle splitting with server-side rendering](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md). +> `React.lazy` و Suspense هنوز برای رندر کردن در سمت سرور در دسترس نیست. اگر شما می خواهید کد تان را در یک برنامه ی رندر شونده در سمت سرور تکه تکه کنید، ما به شما [Loadable Components](https://github.com/smooth-code/loadable-components) را پیشنهاد می کنیم که [راهنمای خوبی برای تکه تکه کردن bundle در رندر سمت سرور](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md) دارد. + +تابع `React.lazy` به شما اجازه می دهد که یک import پویا را به عنوان یک component معمولی رندر کنید. -The `React.lazy` function lets you render a dynamic import as a regular component. +**قبل:** -**Before:** ```js import OtherComponent from './OtherComponent'; ``` -**After:** +**بعد:** ```js const OtherComponent = React.lazy(() => import('./OtherComponent')); ``` -This will automatically load the bundle containing the `OtherComponent` when this component is first rendered. +هنگامی که کامپوننت فوق رندر می شود، بصورت خودکار فایل bundle ای که حاوی `OtherComponent` است را لود می کند. + +`React.lazy` یک تابع به عنوان ورودی می گیرد که یک import() پویا را صدا می کند. که یک promise برمیگرداند که resolves آن یک ماژول با خروجی default ای است که حاوی یک کامپوننت ری‌اکت است. -`React.lazy` takes a function that must call a dynamic `import()`. This must return a `Promise` which resolves to a module with a `default` export containing a React component. +### Suspense {#suspense} -The lazy component should then be rendered inside a `Suspense` component, which allows us to show some fallback content (such as a loading indicator) while we're waiting for the lazy component to load. +ما باید در فاصله ی زمانی ای که `MyComponent` رندر شده است ولی هنوز `OtherComponent` را بارگیری نکرده است، یک محتوا - همانند نشانگر بارگیری - تا زمان لود `OtherComponent` نمایش دهیم. این کار توسط کامپوننت `Suspense` انجام می گیرد. ```js const OtherComponent = React.lazy(() => import('./OtherComponent')); @@ -152,7 +135,7 @@ function MyComponent() { } ``` -The `fallback` prop accepts any React elements that you want to render while waiting for the component to load. You can place the `Suspense` component anywhere above the lazy component. You can even wrap multiple lazy components with a single `Suspense` component. +prop `fallback` هر المان ری‌اکتی که شما می خواهید در بازه ی صبر کردن تا بارگیری کامپوننت اصلی رندر کنید را به عنوان ورودی می پذیرد. شما می توانید هر جایی کامپوننت `Suspense` را در بالای کامپوننت lazy قرار دهید. حتی شما می توانید چندین کامپوننت lazy را داخل یک کامپوننت `Suspense` قرار دهید. ```js const OtherComponent = React.lazy(() => import('./OtherComponent')); @@ -172,9 +155,9 @@ function MyComponent() { } ``` -### Error boundaries {#error-boundaries} +### مرز های خطا (Error Boundaries)‌ {#error-boundaries} -If the other module fails to load (for example, due to network failure), it will trigger an error. You can handle these errors to show a nice user experience and manage recovery with [Error Boundaries](/docs/error-boundaries.html). Once you've created your Error Boundary, you can use it anywhere above your lazy components to display an error state when there's a network error. +اگر یک ماژول در هنگام بارگیری با مشکل مواجه شود (برای مثال، به خاطر مشکلات شبکه)، خطا می دهد. شما می توانید خطا های اینچنینی را هندل کنید که تجربه کاربری خوبی را نشان داده و بازیابی را با [مرز های خطا](/docs/error-boundaries.html) مدیریت کنید. هنگامی که مرز خطای تان را ساختید، شما می توانید از آن در هر جایی در بالای کامپوننت lazy تان برای نمایش حالت خطا هنگامی که مشکلی در شبکه وجود دارد، استفاده کنید. ```js import MyErrorBoundary from './MyErrorBoundary'; @@ -195,19 +178,13 @@ const MyComponent = () => ( ); ``` -## Route-based code splitting {#route-based-code-splitting} +## تکه تکه کردن کد بصورت مسیر محور {#route-based-code-splitting} -Deciding where in your app to introduce code splitting can be a bit tricky. You -want to make sure you choose places that will split bundles evenly, but won't -disrupt the user experience. +تشخیص اینکه در کدام سطح از برنامه تان از تکه تکه کردن کد استفاده کنید می تواند کمی مشکل باشد. شما می خواهید اطمینان حاصل کنید که جاهایی را انتخاب کنید که bundle ها را بصورت مساوری تقسیم کنید، اما تجربه ی کاربر را مختل نکنید. -A good place to start is with routes. Most people on the web are used to -page transitions taking some amount of time to load. You also tend to be -re-rendering the entire page at once so your users are unlikely to be -interacting with other elements on the page at the same time. +یکی از جاهای خوب برای شروع با مسیر (route) هاست. بیشتر افراد در وب از page transitions ها استفاده کرده اند که مقداری زمان می برد تا بارگیری شوند. شما نیز می توانید کل صفحه را به یکباره دوباره رندر کنید تا کاربران نتوانند با سایر المان های صفحه در همان لحظه تعامل کنند. -Here's an example of how to setup route-based code splitting into your app using -libraries like [React Router](https://reacttraining.com/react-router/) with `React.lazy`. +اینجا یک مثال از چگونگی راه اندازی تکه تکه کردن کد بر پایه ی مسیر (route-based code splitting) در داخل برنامه تان با استفاده از کتابخانه هایی مثل [React Router](https://reacttraining.com/react-router/) با `React.lazy` را مشاهده می کنید. ```js import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; @@ -228,9 +205,9 @@ const App = () => ( ); ``` -## Named Exports {#named-exports} +## export های نامگذاری شده {#named-exports} -`React.lazy` currently only supports default exports. If the module you want to import uses named exports, you can create an intermediate module that reexports it as the default. This ensures that tree shaking keeps working and that you don't pull in unused components. +در حال حاضر `React.lazy` صرفا export های پیشفرض را پشتیبانی می کند، اگر ماژولی که شما می خواهید import کنید از export نامگذاری شده استفاده می کند، شما می توانید یک ماژول میانجی ایجاد کنید که آن را بصورت پیشفرض export می کند. این تضمین می کند که ساختار درختی برنامه همچنان بصورت صحیح کار می کند و شما کامپوننت بلا استفاده ای را درخواست نکرده اید. ```js // ManyComponents.js diff --git a/content/docs/hooks-intro.md b/content/docs/hooks-intro.md index 7c0d85210..f04be9fd6 100644 --- a/content/docs/hooks-intro.md +++ b/content/docs/hooks-intro.md @@ -54,7 +54,7 @@ function Example() { **اگر هم‌‌اکنون می‌‌خواهید یادگیری هوک‌‌ها را شروع کنید، [مستقیم به صفحه‌‌ی بعد بروید!](/docs/hooks-overview.html)** البته می‌‌توانید مطالعه‌‌ این صفحه را ادامه دهید و با دلایلی که هوک‌‌ها را به ری‌‌اکت اضافه کردیم و اینکه چگونه بدون بازنویسی اپلیکیشن‌‌های قبلی از آن‌‌ها استفاده خواهیم کرد، آشنا شوید. -## انگیزه +## انگیزه {#انگیزه} هوک‌ها مشکلات مختلفی که ظاهرا به هم ارتباط ندارند را حل می‌کنند. مواردی که ما طی در نوشتن و نگهداری هزاران کامپوننت طی پنج سال با آن مواجه شده‌ایم. اگر درحال یادگیری ری‌اکت هستید، هروز از آن استفاده می‌کنید یا از کتابخانه مشابهی استفاده می‌کنید که بر پایه کامپوننت طراحی شده‌است، احتمالا به این مشکلات بر خورد کرده‌اید.