Skip to content

Why Using focusSelf Inside useEffect Sets Focus to the Last Item Instead of the First — and How to Fix It #180

@M7moudMostafa

Description

@M7moudMostafa

Why, when using focusSelf inside useEffect, does the focus go to the last item in the list, but when using setFocus it correctly goes to the first item? And what is the solution if I need to use it with focusSelf()?

const Item = ({ item, onEnterPress }) => {
  const { ref, focused } = useFocusable({
    focusKey: item.focusKey,
    onEnterPress: () => {
      onEnterPress(item.title);
    },
  });

  useEffect(() => {
    setFocus("Main");
  }, []);

  return (
    <MenuItem ref={ref} $focused={focused}>
      {item.icon} {item.title}
    </MenuItem>
  );
};

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions