AleksandrHovhannisyan/aleksandrhovhannisyan.com

Keeping Focus in Place with Load-More Buttons

Opened this issue ยท 3 comments

Keeping Focus in Place with Load-More Buttons

Awesome post! Thanks for sharing ๐Ÿ˜„

Great blog post. Helped a lot. Only one suggestion: You can simplify the code a bit by using a ref callback. That way you don't need the useEffect either ๐Ÿ˜„

const ResultGrid = (props) => {
  const onMount = (ref) => {
    if (ref) {
      ref.focus();
    }
  }

  return (
    <>
      <ol>
        {props.results.map((result, i) => {
          const isFirstNewResult = i === props.firstNewResultIndex;
          return (
            <li key={i} id={`result-${i}`}>
              <a
                ref={isFirstNewResult ? onMount : undefined}

@jkettmann Clever, thanks for sharing! I'll see if I can get around to updating the article with that approach.