Read here. Code on desktop. The coding workspace opens on screens 1024px and wider.
JavaScript interview question
useFocus
easy10 min
Return an element ref and a stable function that focuses the element currently assigned to it.
Requirements
Implement useFocus(), returning [ref, focus]. The initial ref.current is null. Attach ref to a focusable HTML element in a React component.
Calling focus invokes focus() on the currently referenced element. If ref.current is null, it safely does nothing and returns undefined.
Keep both the ref object and focus function identical across rerenders. An earlier captured focus function must use the latest ref.current, including when the element changes.
Do not focus automatically on mount. Instances must have independent refs. TypeScript should support a generic HTMLElement subtype, such as useFocus<HTMLInputElement>().
No focus-trapping, automatic selection, or custom HTML/CSS implementation is required.
Examples
Example 1
Input: const [ref, focus] = useFocus(); // attach ref to an input; then focus()
Output: The referenced input becomes document.activeElement.
A tuple containing a ref object and a no-argument focus utility.
What the tests check
Starts with a null ref and a callable focus utility