From 9a1a49c0f24e8720ec824b8c3dd3bba962631d9a Mon Sep 17 00:00:00 2001 From: Gasim Gasimzada Date: Sat, 30 Mar 2019 15:31:05 +0400 Subject: [PATCH 1/3] Translate error boundaries --- content/docs/error-boundaries.md | 108 +++++++++++++++---------------- 1 file changed, 52 insertions(+), 56 deletions(-) diff --git a/content/docs/error-boundaries.md b/content/docs/error-boundaries.md index 147732911..8519cfd21 100644 --- a/content/docs/error-boundaries.md +++ b/content/docs/error-boundaries.md @@ -1,28 +1,27 @@ --- id: error-boundaries -title: Error Boundaries +title: Xəta Sərhədləri permalink: docs/error-boundaries.html --- -In the past, JavaScript errors inside components used to corrupt React’s internal state and cause it to [emit](https://github.com/facebook/react/issues/4026) [cryptic](https://github.com/facebook/react/issues/6895) [errors](https://github.com/facebook/react/issues/8579) on next renders. These errors were always caused by an earlier error in the application code, but React did not provide a way to handle them gracefully in components, and could not recover from them. +Keçmişdə, komponent daxilində baş verən Javascript xətaları, React-in daxili vəziyyətini korlayıb, sonrakı renderlərdə [kriptik](https://github.com/facebook/react/issues/6895) [xətalar](https://github.com/facebook/react/issues/8579) [göstərirdi](https://github.com/facebook/react/issues/4026). Bu xətaların həmişə applikasiya kodunda əvvəlki xətalara görə baş verməyinə baxmayaraq, React bu xətaların komponent daxilində idarəsi üçün və bu xətalardan bərpa olunmaq üçün heç bir mexanizm təmin etmirdi. +## Xəta Sərhədlərinin Təqdimi {#introducing-error-boundaries} -## Introducing Error Boundaries {#introducing-error-boundaries} +UI-ın bir hissəsində baş verən Javascript xətası bütün applikasiyanı sındırmamalıdır. Bu problemin React-də həll olunması üçün, React 16 "xəta sərhədi" adında yeni bir konsepsiya təqdim edir. -A JavaScript error in a part of the UI shouldn’t break the whole app. To solve this problem for React users, React 16 introduces a new concept of an “error boundary”. +Xəta sərhədləri **uşaq komponent ağacında baş verən Javascript xətaları tutan, bu xətaları qeydiyyata alan, və xəta olduqda fallback UI göstərən** bir React komponentidir. Xəta sərhədləri render zamanı, lifecycle metodlarında, və bütün altında olan ağacdakı konstruktorlarda baş verən xətaları tutur. -Error boundaries are React components that **catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI** instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them. - -> Note +> Qeyd > -> Error boundaries do **not** catch errors for: +> Xəta sərhədləri aşağıda baş verən xətaları **tutmur**: > -> * Event handlers ([learn more](#how-about-event-handlers)) -> * Asynchronous code (e.g. `setTimeout` or `requestAnimationFrame` callbacks) -> * Server side rendering -> * Errors thrown in the error boundary itself (rather than its children) +> * Hadisə işləyicilərində ([daha ətraflı](#how-about-event-handlers)) +> * Asinxron kod (məsələn `setTimeout` və ya `requestAnimationFrame` callback-ləri) +> * Serverdə render edilməsi zamanı +> * Xəta sərhədinin daxilində atılan xətalar (uşaqda atılmağın yerinə) -A class component becomes an error boundary if it defines either (or both) of the lifecycle methods [`static getDerivedStateFromError()`](/docs/react-component.html#static-getderivedstatefromerror) or [`componentDidCatch()`](/docs/react-component.html#componentdidcatch). Use `static getDerivedStateFromError()` to render a fallback UI after an error has been thrown. Use `componentDidCatch()` to log error information. +Klass komponenti göstərilən [`static getDerivedStateFromError()`](/docs/react-component.html#static-getderivedstatefromerror) və ya [`componentDidCatch()`](/docs/react-component.html#componentdidcatch) lifecycle metodlarının birini (və ya hər ikisini) tətbiq etdikdə xəta sərhədinə çevrilir. `static getDerivedStateFromError()` funskiyasını xəta atıldıqdan sonra fallback UI render etmək üçün işlədin. `componentDidCatch()` funskiyasını xətaları qeydiyyata almaq üçün işlədin. ```js{7-10,12-15,18-21} class ErrorBoundary extends React.Component { @@ -32,19 +31,19 @@ class ErrorBoundary extends React.Component { } static getDerivedStateFromError(error) { - // Update state so the next render will show the fallback UI. + // Gələn renderdə UI göstərmək üçün state-i yeniləyin. return { hasError: true }; } componentDidCatch(error, info) { - // You can also log the error to an error reporting service + // Siz həmçinin xətaları, xəta hesabat servislərində qeydiyyata ala bilərsiniz logErrorToMyService(error, info); } render() { if (this.state.hasError) { - // You can render any custom fallback UI - return

Something went wrong.

; + // Siz hər hansı bir fallback UI render edə bilərsiniz + return

Bir şey yalnış getdi.

; } return this.props.children; @@ -52,7 +51,7 @@ class ErrorBoundary extends React.Component { } ``` -Then you can use it as a regular component: +Sonra siz bunu normal komponent kimi işlədə bilərsiniz: ```js @@ -60,53 +59,50 @@ Then you can use it as a regular component: ``` -Error boundaries work like a JavaScript `catch {}` block, but for components. Only class components can be error boundaries. In practice, most of the time you’ll want to declare an error boundary component once and use it throughout your application. - -Note that **error boundaries only catch errors in the components below them in the tree**. An error boundary can’t catch an error within itself. If an error boundary fails trying to render the error message, the error will propagate to the closest error boundary above it. This, too, is similar to how catch {} block works in JavaScript. +Xəta sərhədləri komponentlər üçün Javascript-in `catch {}` bloku kimi işləyirlər. Yalnız klas komponentləri xəta sərhədləri ola bilərlər. Praktikada, bir çox zaman, siz xəta sərhədi komponentini bir dəfə tətbiq edib bütün applikasiya zamanı işlətmək istəyərsiniz. -## Live Demo {#live-demo} +Qeyd edək ki, **xəta sərhədləri yalnız altındaki ağacda olan komponentlərin xətalarını tuta bilir**. Xəta sərhədi daxilində baş verən xətanı tuta bilmir. Əgər xəta sərhədin xəta mesajını render edə bilmirsə, xəta bu komponentin yuxarısında olan ən yaxın xəta sərhədinə yayılacaq. Javascriptin catch {} funskiyasıda bunun kimi işləyir. -Check out [this example of declaring and using an error boundary](https://codepen.io/gaearon/pen/wqvxGa?editors=0010) with [React 16](/blog/2017/09/26/react-v16.0.html). +## Canlı Demo {#live-demo} +[React 16-da](/blog/2017/09/26/react-v16.0.html) [xəta sərhədinin yaranması və işlənməsi misalına](https://codepen.io/gaearon/pen/wqvxGa?editors=0010) baxın. -## Where to Place Error Boundaries {#where-to-place-error-boundaries} -The granularity of error boundaries is up to you. You may wrap top-level route components to display a “Something went wrong” message to the user, just like server-side frameworks often handle crashes. You may also wrap individual widgets in an error boundary to protect them from crashing the rest of the application. +## Xəta Sərhədlərini Harada Yerləşdirək {#where-to-place-error-boundaries} +Xəta sərhədlərinin zənginliyi / əhatəsi sizdən asılıdır. Siz ən yuxarıda olan route komponentlərini əhatə edib, server tərəfində işlənilən freymvorklardakı kimi, "Bir şey yalnış getdi" mesajını istifadəçiyə göstərə bilərsiniz. Siz həmçinin fərdi vidcetlərini xəta sərhədləri ilə əhatə edib xətaların bütün applikasiyanı sındırmasından qoruya bilərsiniz. -## New Behavior for Uncaught Errors {#new-behavior-for-uncaught-errors} +## Tutulmamış Xətalar üçün Yeni Davranış {#new-behavior-for-uncaught-errors} -This change has an important implication. **As of React 16, errors that were not caught by any error boundary will result in unmounting of the whole React component tree.** +Bu dəyişikliyin vacib bir təsiri var. **React 16-dan başlayaraq, xəta sərhədində tutulmayan xətalar, React komponent ağacının bütünlüklə DOM-dan çıxarılmasına səbən olacaq.** -We debated this decision, but in our experience it is worse to leave corrupted UI in place than to completely remove it. For example, in a product like Messenger leaving the broken UI visible could lead to somebody sending a message to the wrong person. Similarly, it is worse for a payments app to display a wrong amount than to render nothing. +Biz bu qərarı müzakirə etdik və bizim təcrübəmizdən korlanmış UI-ı yerində saxlamaq, bu UI-ı bütün silməkdən daha pisdir. Məsələn, Messenger kimi bir produktda, sınmış UI-ı göstərmək, kiminzə yalnış adama mesaj göndərməsinə səbəb ola bilər. Eyniliklə, ödəmə applikasiyasının yalnız dəyər göstərməsi heç nə göstərməsindən daha pisdir. -This change means that as you migrate to React 16, you will likely uncover existing crashes in your application that have been unnoticed before. Adding error boundaries lets you provide better user experience when something goes wrong. +Bu dəyişikliklə siz React 16-a miqrasiya etdikdə, sizin applikasiyanızda əvvəl fikir vermədiyiniz mövcud xətaların üstünü aça biləcəksiniz. Xəta sərhədlərini əlavə etməklə bir şey yalnış getdikdə daha yaxşı istifadəçi təcrübəsi yarada bilərsiniz. -For example, Facebook Messenger wraps content of the sidebar, the info panel, the conversation log, and the message input into separate error boundaries. If some component in one of these UI areas crashes, the rest of them remain interactive. +Məsələn, Facebook Messenger sidebar-ı, məlumat panelini, chat yazılarını, və mesaj daxil etməsini ayrılıqda xəta sərhədləri ilə əhətə edir. Əgər hər hansı bir UI sahəsində bir komponent sınırsa, applikasiyanın qalanı interaktiv qalır. -We also encourage you to use JS error reporting services (or build your own) so that you can learn about unhandled exceptions as they happen in production, and fix them. +Biz həmçinin sizin Javascript xəta servislərindən (və ya özünüz düzəldin) istifadə etməyi tövsiyyə edirik. Bu servislər ilə production-da baş verən xətaları tapıb, bu xətaları düzədə bilərsiniz. +## Komponent Stek İzləri {#component-stack-traces} -## Component Stack Traces {#component-stack-traces} +React 16 render zamanı baş verən bütün xətaları, applikasiya təsadüfən bu xətaları udsa belə, development zamanı brauzerin konsoluna yazır. Xəta mesajı və Javascript stekindən əlavə, React 16 həmçinin komponent stek izlərini göstərir. İndi siz xətanın komponent ağacında dəqiq harada baş verdiyini görə bilərsiniz: -React 16 prints all errors that occurred during rendering to the console in development, even if the application accidentally swallows them. In addition to the error message and the JavaScript stack, it also provides component stack traces. Now you can see where exactly in the component tree the failure has happened: +Xətanın Error Boundary komponenti tərəfindən tutulması -Error caught by Error Boundary component +Siz komponentin stek izində fayl adlarını və sətir nömrələrini də görə bilərsiniz. Bu [Create React App](https://github.com/facebookincubator/create-react-app) layihələrində birbaşa işləyir: -You can also see the filenames and line numbers in the component stack trace. This works by default in [Create React App](https://github.com/facebookincubator/create-react-app) projects: +Xətanın Error Boundary komponenti tərəfindən sətir nömrələri ilə tutulması -Error caught by Error Boundary component with line numbers +Əgər siz Create React App istifadə etmirsinizsə, siz [bu plugini](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) əllə Babel konfiqurasiyasına əlavə edə bilərsiniz. Qeyd edək ki, bu yalnız development zamanı işlətmək üçündür və **production-də söndürülməlidir**. -If you don’t use Create React App, you can add [this plugin](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) manually to your Babel configuration. Note that it’s intended only for development and **must be disabled in production**. - -> Note +> Qeyd > -> Component names displayed in the stack traces depend on the [`Function.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) property. If you support older browsers and devices which may not yet provide this natively (e.g. IE 11), consider including a `Function.name` polyfill in your bundled application, such as [`function.name-polyfill`](https://github.com/JamesMGreene/Function.name). Alternatively, you may explicitly set the [`displayName`](/docs/react-component.html#displayname) property on all your components. - +> Komponent adları stek izlərində [`Function.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) parametrindən asılıdır. Əgər siz bu parametri nativ formada dəstəkləməyən köhnə brauzerləri və cihazları dəstəkləyirsinizsə (məsələn IE 11), `Function.name` polifilini (məsələn [`function.name-polyfill`](https://github.com/JamesMGreene/Function.name)) paketlənmiş applikasiyanıza əlavə edin. Alternativ olaraq, siz açıq şəkildə `displayName` parametrini bütün komponentlərdə təyin edə bilərsiniz. -## How About try/catch? {#how-about-trycatch} +## Bəs try/catch? {#how-about-trycatch} -`try` / `catch` is great but it only works for imperative code: +`try` / `catch` əladır amma yalnız imperativ kod ilə işləyir: ```js try { @@ -116,21 +112,21 @@ try { } ``` -However, React components are declarative and specify *what* should be rendered: +Lakin, React komponentlər deklarativdirlər və **nəyin** render olunacağını müəyyənləşdirirlər: ```js