Reputation: 167
I need to add scroll to first error in form. I found few solutions:
extra package - doesn't work
Code from discussion here
<FormSpy
subscription={{ submitFailed: true }}
onChange={() => {
const el = document.querySelector('.error')
if (el) {
el.scrollIntoView({ behavior: 'smooth' })
}
}}
/>
My problem is that second case works only second time. Before I submit first time error
className doesn't exist yet, it appears only after submit, and el
is null
. But second and next times it works all the time, if I not remove error className from render.
How can I solve it?
Upvotes: 1
Views: 1351