diff --git a/content/docs/faq-functions.md b/content/docs/faq-functions.md index f03b2ba76..07cf0e4b3 100644 --- a/content/docs/faq-functions.md +++ b/content/docs/faq-functions.md @@ -1,26 +1,26 @@ --- id: faq-functions -title: Passing Functions to Components +title: تمرير الدوال إلى المكونات permalink: docs/faq-functions.html layout: docs category: FAQ --- -### How do I pass an event handler (like onClick) to a component? {#how-do-i-pass-an-event-handler-like-onclick-to-a-component} +### كيف يمكنني تمرير مُعالِج حدث (مثل onClick) إلى مكوّن؟ {#how-do-i-pass-an-event-handler-like-onclick-to-a-component} -Pass event handlers and other functions as props to child components: +مرِّر مُعالِجات الأحداث والدوال الأخرى كخاصيّات `props` إلى المكوّنات الأبناء: ```jsx ; + return ; } } ``` -#### Class Properties (Stage 3 Proposal) {#class-properties-stage-3-proposal} +#### خاصيّات الصنف (اقتراح المرحلة 3) {#class-properties-stage-3-proposal} ```jsx class Foo extends Component { - // Note: this syntax is experimental and not standardized yet. + // ملاحظة: هذه الصياغة تجريبية وليست معيارية بعد handleClick = () => { - console.log('Click happened'); + console.log('حدثت نقرة'); } render() { - return ; + return ; } } ``` -#### Bind in Render {#bind-in-render} +#### الربط في تابع التصيير `render` {#bind-in-render} ```jsx class Foo extends Component { handleClick() { - console.log('Click happened'); + console.log('حدثت نقرة'); } render() { - return ; + return ; } } ``` ->**Note:** +>**ملاحظة:** > ->Using `Function.prototype.bind` in render creates a new function each time the component renders, which may have performance implications (see below). +>يُؤدّي استخدام `Function.prototype.bind` في التابع `render` إلى إنشاء دالة جديدة في كل مرّة يُصيَّر فيها المكوّن، ممّا قد يؤثر على الأداء (للمزيد تابع في الأسفل). -#### Arrow Function in Render {#arrow-function-in-render} +#### استخدام الدوال السهميّة في تابع التصيير `render` {#arrow-function-in-render} ```jsx class Foo extends Component { handleClick() { - console.log('Click happened'); + console.log('حدثت نقرة'); } render() { - return ; + return ; } } ``` ->**Note:** +>**ملاحظة:** > ->Using an arrow function in render creates a new function each time the component renders, which may break optimizations based on strict identity comparison. +> يُؤدّي استخدام الدوال السهميّة في التابع `render` إلى إنشاء دالة جديدة في كل مرّة يُصيَّر فيها المكوّن، ممّا قد يؤثر على الأداء. -### Is it OK to use arrow functions in render methods? {#is-it-ok-to-use-arrow-functions-in-render-methods} +### هل من الجيّد استخدام الدوال السهميّة في توابع التصيير؟ {#is-it-ok-to-use-arrow-functions-in-render-methods} -Generally speaking, yes, it is OK, and it is often the easiest way to pass parameters to callback functions. +بشكلٍ عام نعم، لا مشكلة في ذلك، وهي عادةً الطريقة الأسهل لتمرير المُعامِلات إلى دوال ردود النداء. -If you do have performance issues, by all means, optimize! +إن كانت لديك مشاكل في الأداء، فحاول تحسينه عن طريق الطرق المشروحة في [المستندات](/docs/getting-started.html). -### Why is binding necessary at all? {#why-is-binding-necessary-at-all} +### لماذا من الضروري إجراء الربط أساسًا؟ {#why-is-binding-necessary-at-all} -In JavaScript, these two code snippets are **not** equivalent: +في JavaScript **لا** تكون الشيفرتان التاليتان متساويتين: ```js obj.method(); @@ -104,47 +104,47 @@ var method = obj.method; method(); ``` -Binding methods helps ensure that the second snippet works the same way as the first one. +تضمن توابع الربط عمل الشيفرة الثانية بنفس طريقة عمل الشيفرة الأولى. -With React, typically you only need to bind the methods you *pass* to other components. For example, ` + return } ``` -Instead, *pass the function itself* (without parens): +بدلًا من فعل ذلك *مرِّر الدالة نفسها* بدون أقواس: ```jsx render() { // Correct: handleClick is passed as a reference! - return + return } ``` -### How do I pass a parameter to an event handler or callback? {#how-do-i-pass-a-parameter-to-an-event-handler-or-callback} +### كيف أمرّر مُعامِل إلى مُعالِج الأحداث أو رد النداء(callback)؟ {#how-do-i-pass-a-parameter-to-an-event-handler-or-callback} -You can use an arrow function to wrap around an event handler and pass parameters: +تستطيع استخدام الدوال السهميّة من أجل الالتفاف حول مُعالِجات الأحداث وتمرير المُعامِلات: ```jsx ; + return ; } handleClick() { @@ -260,9 +260,9 @@ class LoadMoreButton extends React.Component { } ``` -#### Debounce {#debounce} +#### منع الارتداد (Debounce) {#debounce} -Debouncing ensures that a function will not be executed until after a certain amount of time has passed since it was last called. This can be useful when you have to perform some expensive calculation in response to an event that might dispatch rapidly (eg scroll or keyboard events). The example below debounces text input with a 250ms delay. +يضمن منع الارتداد عدم تنفيذ الدالة حتى مرور فترة معينة من الوقت منذ آخر استدعاء لها. يكون هذا مفيدًا عندما يتوجب عليك إجراء بعض الحسابات المكلفة استجابةً لحدث قد ينتهي بسرعة (مثل النزول بالصفحة scroll أو أحداث لوحة المفاتيح). يمنع المثال التالي الارتداد في حقل إدخال نصي مع تأخير 250 ميلي ثانية. ```jsx import debounce from 'lodash.debounce'; @@ -302,13 +302,13 @@ class Searchbox extends React.Component { } ``` -#### `requestAnimationFrame` throttling {#requestanimationframe-throttling} +#### الخنق باستخدام `requestAnimationFrame` {#requestanimationframe-throttling} -[`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) is a way of queuing a function to be executed in the browser at the optimal time for rendering performance. A function that is queued with `requestAnimationFrame` will fire in the next frame. The browser will work hard to ensure that there are 60 frames per second (60 fps). However, if the browser is unable to it will naturally *limit* the amount of frames in a second. For example, a device might only be able to handle 30 fps and so you will only get 30 frames in that second. Using `requestAnimationFrame` for throttling is a useful technique in that it prevents you from doing more than 60 updates in a second. If you are doing 100 updates in a second this creates additional work for the browser that the user will not see anyway. +إنّ [`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) هو عبارة عن طريقة لوضع الدالة في طابور لتنفيذه في المتصفح في الوقت المثالي لتحسين أداء التصيير. تُطلَق الدالة التي توضع في الطابور باستخدام `requestAnimationFrame` في الإطار الزمني التالي. سيعمل المتصفح بجد لضمان الحصول على 60 إطار في الثانية (60 fps). إن لم يكن المتصفح قادرًا على ذلك فسـ*يحد* عدد الإطارات في الثانية. على سبيل المثال قد يكون جهازك قادر على التعامل فقط مع 30 إطار بالثانية لذا ستحصل على 30 إطار بالثانية فقط. إنّ استخدام `requestAnimationFrame` للخنق هو تقنية مفيدة تمنع من إجراء أكثر من 60 تحديث في الثانية. إن كنت تجري 100 تحديث في الثانية فسيؤدي ذلك إلى إنشاء عمل إضافي على المتصفح والذي لن يراه المستخدم على أية حال. ->**Note:** +>**ملاحظة:** > ->Using this technique will only capture the last published value in a frame. You can see an example of how this optimization works on [`MDN`](https://developer.mozilla.org/en-US/docs/Web/Events/scroll) +>استخدام هذه التقنية سيلتقط فقط آخر قيمة منشورة في الإطار. بإمكانك رؤية مثال حول كيفية عمل هذا الضبط من [هنا](https://developer.mozilla.org/en-US/docs/Web/Events/scroll). ```jsx import rafSchedule from 'raf-schd'; @@ -319,20 +319,20 @@ class ScrollListener extends React.Component { this.handleScroll = this.handleScroll.bind(this); - // Create a new function to schedule updates. + // إنشاء دالة جديدة لجدولة التحديثات this.scheduleUpdate = rafSchedule( point => this.props.onScroll(point) ); } handleScroll(e) { - // When we receive a scroll event, schedule an update. - // If we receive many updates within a frame, we'll only publish the latest value. + // عند استقبال حدث تمرير, جدول تحديثًا + // إن استقبلنا الكثير من التحديثات ضمن الإطار فسننشر آخر قيمة فقط this.scheduleUpdate({ x: e.clientX, y: e.clientY }); } componentWillUnmount() { - // Cancel any pending updates since we're unmounting. + // إلغاء أي تحديثات منتظرة بما أننا سنفصل المكون this.scheduleUpdate.cancel(); } @@ -349,6 +349,6 @@ class ScrollListener extends React.Component { } ``` -#### Testing your rate limiting {#testing-your-rate-limiting} +#### اختبار حدود معدل تحديث الإطار لديك {#testing-your-rate-limiting} -When testing your rate limiting code works correctly it is helpful to have the ability to fast forward time. If you are using [`jest`](https://facebook.github.io/jest/) then you can use [`mock timers`](https://facebook.github.io/jest/docs/en/timer-mocks.html) to fast forward time. If you are using `requestAnimationFrame` throttling then you may find [`raf-stub`](https://github.com/alexreardon/raf-stub) to be a useful tool to control the ticking of animation frames. +عند اختبار حدود معدل تحديث الإطار من المفيد امتلاك القدرة على تمرير الزمن بسرعة. إن كنت تستخدم [`jest`](https://facebook.github.io/jest/) بإمكانك استخدام [`محاكيات المؤقّتات (mock timers)`](https://facebook.github.io/jest/docs/en/timer-mocks.html) لتمرير الوقت بسرعة. إن كنت تستخدم الخنق عن طريق `requestAnimationFrame` فهنالك الأداة [`raf-stub`](https://github.com/alexreardon/raf-stub) مفيدة للتحكم بضبط تحريك الإطارات.