79828270

Date: 2025-11-24 04:46:13
Score: 0.5
Natty:
Report link

Why your snippet works but your app doesn't:
In your Stack Snippet, the focus happens during initial page load (which browsers sometimes treat as "user-initiated"), but in your real app, the focus occurs after a user action (like clicking "edit"), and React's useEffect delays it just enough for the browser to consider it non-user-driven.

The fix
Force the browser to treat focus as user-driven by focusing synchronously within the user's click event (not in useEffect).

const EditableCell = () => {
  const inputRef = useRef(null);

  const handleEditStart = (e) => {
    inputRef.current?.focus();
  };

  return (
    <td onClick={handleEditStart}>
      <div 
        ref={inputRef}
        role="listbox"
        tabIndex="0"
        className="focusable"
      >
        Content
      </div>
    </td>
  );
};
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): Why you
  • Low reputation (1):
Posted by: Mir_dw_work