0Pricing
Web Accessibility Academy · درس

إدارة التركيز باستخدام Refs وuseEffect

تحكموا في التركيز ضمن عالم قائم على المكوّنات

إدارة التركيز باستخدام Refs وuseEffect درس مجاني في Web Accessibility Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web Accessibility Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web Accessibility Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Focus Needs Managing

When UI appears, disappears, or updates, the keyboard focus can land in the wrong place or vanish. In React you steer it deliberately with refs and effects.

A ref Points to a DOM Node

useRef gives you a stable handle to a real element. After render, ref.current is the actual DOM node you can call .focus() on.

const inputRef = useRef(null);
<input ref={inputRef} />

useEffect Runs After Render

Focus must happen once the element exists in the DOM. useEffect runs after the browser paints, so it is the safe place to move focus.

useEffect(() => {
  inputRef.current.focus();
}, []);

Focus on Mount

An empty dependency array means the effect runs once when the component mounts. Use this to put focus on the first field of a form or step.

Focus When Something Opens

Add a dependency like isOpen so the effect re-runs when state flips. When a panel opens, you can move focus straight into it.

useEffect(() => {
  if (isOpen) panelRef.current.focus();
}, [isOpen]);

Guard Against Null

A ref can be null if the node is not rendered yet. Check ref.current exists before calling .focus() to avoid a crash on a missing element.

ref.current?.focus();

Make Non-Inputs Focusable

Headings and divs are not focusable by default. Add tabindex of -1 so script can focus them without adding them to the natural tab order.

<div tabindex="-1" ref={regionRef}>...</div>

Restore Focus on Cleanup

An effect can return a cleanup function. Use it to send focus back to the trigger when a layer unmounts, so users are not dropped at the page top.

useEffect(() => {
  const prev = document.activeElement;
  return () => prev.focus();
}, []);

Beware Stale Closures

An effect captures values from its render. If you focus based on data, list it in the dependency array so the effect uses fresh values, not stale ones.

forwardRef for Child Elements

To focus an element inside a child component, wrap it in forwardRef. The parent then holds a ref to the inner node and can focus it.

const Field = forwardRef((props, ref) =>
  <input ref={ref} {...props} />);

Do Not Over-Focus

Move focus only when it truly helps the user, like opening a dialog or showing an error. Grabbing focus on every render frustrates everyone.

Quick Check

You want to move keyboard focus to an element right after React renders it. Where does that belong?

Recap: Refs Plus Effects

Pair a useRef with a useEffect to move focus after render. Guard against null, restore focus on cleanup, and only steer focus when it helps. 🎯

الأسئلة الشائعة

هل درس «إدارة التركيز باستخدام Refs وuseEffect» مجاني؟

نعم — نص درس «إدارة التركيز باستخدام Refs وuseEffect» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web Accessibility Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Web Accessibility Academy 4 دروس في المجموع.

ماذا ستتعلم في «إدارة التركيز باستخدام Refs وuseEffect»؟

تحكموا في التركيز ضمن عالم قائم على المكوّنات تتمرن على Web Accessibility Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Web Accessibility Academy؟

لا تُشترط خبرة سابقة. Web Accessibility Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «إدارة التركيز باستخدام Refs وuseEffect»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Web Accessibility Academy هذا؟

نعم. كل درس في Web Accessibility Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تغيير المسارات دون إعادة تحميل الصفحات
  2. إدارة التركيز باستخدام Refs وuseEffect
  3. Fragments وPortals وفخ ترتيب DOM
  4. jsx-a11y وحواجز الأمان على مستوى المكوّنات
← العودة إلى Web Accessibility Academy