React - usePrevious

Hi,
Can someone please explain this function?
My brain almost melted in an attempt to understand the function

function usePrevious(value) {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
}

from here: https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_accessibility

in addition, we could replace

const [isEditing, setEditing] = useState(false);

by:

const [isEditing, setEditing] = useState();

and change this:

useEffect(() => {
  if (isEditing) {
    editFieldRef.current.focus();
  } else {
    editButtonRef.current.focus();
  }
}, [isEditing]);

to:

  useEffect(() => {
    if (isEditing === true) {
      editFieldRef.current.focus();
    } else if (isEditing === false) {
      editButtonRef.current.focus();
    }
  }, [isEditing]);

instead of using wasEditing

Thanks alot,

Hi @Rafael_Green. Apologies for taking so long to reply to you on all these posts you made a couple of months ago. I have finally caught up with my backlog, just about! I’m going to try to answer a couple of your postings each day.

In the particiular case of this one, I had another look back at the page, and it does include a link to https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state, which in turns links to https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables.

The overall section explains basically what the code is trying to do, and the linked documents help with what the particular bits of it are doing.

In terms of making changes here, I’d rather not have to make changes unless something is specifically wrong, or reallt confusing. This looks like a fairly minor change suggestions, and I still have limited time in which to review things and do rewriting.

No problem at all. Thanks for looking at it