小编ant*_* lu的帖子

如何在 React 中的 ref 上调用scrollIntoView

当我调用 someFun 时,我想让 formRef 在屏幕上可见(它位于页面顶部,当我调用 someFun 时我可能已经滚动到底部),有标准方法可以做到这一点吗?

function List(props) {

    const formRef = useRef(null);


    const someFun = params => {

        if (formRef && formRef.current) {
      

          //error FormRef.current.scrollIntoView is not a function
          formRef.current.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
        }
    }
}


Run Code Online (Sandbox Code Playgroud)

这是上面父组件中使用的表单组件:它是一个ant design表单组件:链接在这里 https://3x.ant.design/components/form/#header

import { Form, Icon, Input, Button, Checkbox } from 'antd';

class NormalLoginForm extends React.Component {
  handleSubmit = e => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      } …
Run Code Online (Sandbox Code Playgroud)

reactjs react-ref

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

react-ref ×1

reactjs ×1