简体   繁体   English

反应模态滚动到顶部

[英]React modal scroll to top

I have a modal with a long form in my react application.我的反应应用程序中有一个长形式的模式。 So when I submit the form I am showing the validation messages from the server on top of the form.因此,当我提交表单时,我会在表单顶部显示来自服务器的验证消息。 So the user has to scroll to the top to view the messages.所以用户必须滚动到顶部才能查看消息。 So I want to automatically scroll to the top when the message appears.所以我想在消息出现时自动滚动到顶部。 So I added the below code in the submit handler function. But it is not working.所以我在提交处理程序 function 中添加了以下代码。但它不起作用。

setAddModalErrorMsg([{ msg: res.data.msg, type: "error" }])
                    window.scrollTo({
                        top: 0,
                        left: 0,
                        behavior: "smooth"
                      });

The other answers showed how you can scroll the modal to the top, and that is the generally accepted way of achieving this, though, I want to show you how to scroll the "Message" into view, regardless of whether it's on the top or not.其他答案显示了如何将模式滚动到顶部,这是实现此目的的普遍接受的方式,不过,我想向您展示如何将“消息”滚动到视图中,无论它是在顶部还是不是。

You would also need to create a ref to where you display your message and use the scrollIntoView functionality to scroll the modal to your validation message.您还需要创建一个指向显示消息的位置的ref ,并使用scrollIntoView功能将模态滚动到您的验证消息。

import React, { useRef } from 'react';

const Modal = () => {
  const validationMessageRef = useRef();

  const setAddModalErrorMsg = () => {
    // scrolls the validation message into view, and the block: 'nearest' ensures it scrolls the modal and not the window
    validationMessageRef.current?.scrollIntoView({ block:'nearest' });
  }

  return (
    <div>
      <div ref={validationMessageRef}>
        // your validation message is displayed here
      </div>

      // rest of your modal content here
    </div>

  )
}

You're trying to scroll window, but chances are your window is already at the top, it's your modal element that needs to scroll up.您正在尝试滚动 window,但很可能您的 window 已经在顶部,这是您需要向上滚动的模态元素。

To do this, i'd create a reference to the modal element, then in your function scroll the modal element via the ref, so something along the lines of:为此,我将创建对模态元素的引用,然后在您的 function 中通过 ref 滚动模态元素,因此如下所示:

import React, {useRef} from 'react';

const Modal = (props) => {
  // use the useRef hook to store a reference to the element
  const modalRef = useRef();
  
  const setAddModalErrorMsg = () => {
    // check the ref exists (it should always exist, it's declared in the JSX below), and call a regular javascript scrollTo function on it
    modalRef.current?.scrollTo({x: 0, y: 0, animated: false});
  }
  
  // see here we create a reference to the div that needs scrolled
  return (
    <div ref={modalRef}>
     { // your modal content }
    </div>
  )

}

to automatically scroll to the top we can use the below code:要自动滚动到顶部,我们可以使用以下代码:

  constructor(props) {
    super(props)
    this.myRef = React.createRef()   // Create a ref object 
    }

add the scrollTo function after setAddModalErrorMsg.在 setAddModalErrorMsg 之后添加 scrollTo function。

 setAddModalErrorMsg([{ msg: res.data.msg, type: "error" }])
    this.myRef.current.scrollTo(0, 0);

 <div ref={this.myRef}></div> 

attach the ref property to a top dom element将 ref 属性附加到顶级 dom 元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM