Skip to content

How to scroll to field with input error after submit? #3835

Answered by logaretm
aslanovshm asked this question in Q&A
Discussion options

You must be logged in to vote

The docs doesn't really discourage you from using refs if they work for you then sure. They only discourage it for doing form actions.

In v4 there is a concept of invalid submissions with that you could easily implement scroll behavior.

function onInvalidSubmit({ values, errors, results }) {
  const firstErrorFieldName = Object.keys(errors)[0];
  const el = document.querySelector(`[name="${firstErrorFieldName}"]`);
  if (el) {
    el.scrollIntoView();
  }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by aslanovshm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants