admin管理员组文章数量:1287828
I am designing the registration page in Reactjs .and I am doing manual validation. now what I want is after clicking onSubmit, the page should scroll to the top to show all errors in the validation.I tried but failed to achieve this.is there anyone who will help me
I am designing the registration page in Reactjs .and I am doing manual validation. now what I want is after clicking onSubmit, the page should scroll to the top to show all errors in the validation.I tried but failed to achieve this.is there anyone who will help me
Share Improve this question asked Apr 27, 2021 at 12:58 Technical knowledgeTechnical knowledge 2222 silver badges11 bronze badges 1-
You can add
id
to an element and usehref
to that particular id. – Bikki Mahato Commented Apr 27, 2021 at 13:13
2 Answers
Reset to default 6If you task is to scroll to errors rather than scrolling to top, you can try this.
import React { useRef } from 'react';
const Component = () => {
const errorRef = useRef(null);
const onSubmitHandler = () => {
...
errorRef.current.scrollIntoView();
}
return (
...
<div ref={errorRef} className='error-container'>
...
</div>
...
);
}
Note: Still you want to try scroll to top you can try this.
window.scrollTo(0,0);
or
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
This line of code works for me
window.scrollTo(0, 0);
本文标签: javascriptscroll to the top of page in reactjsStack Overflow
版权声明:本文标题:javascript - scroll to the top of page in reactjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741265560a2368395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论