反应滚动组件以查看内部溢出隐藏元素

Den*_*nis 3 javascript scroll reactjs

我有一个包含一系列项目的列表,但我无法让它将另一个项目滚动到视图中。诸如此类的解决方案不太有效,因为我必须上下滚动(基本上,如果列表太大,则每隔一段时间滚动它以显示所有项目)。

我制作了一个codepen来说明react-scroll-to-component的问题,但我对任何库/本机解决方案持开放态度。基本功能只是scrollToComponent(ref, <options>).

据我所知,错误是它尝试在主体上滚动,而不是在实际的容器上滚动。如果删除 div 的高度/溢出属性,它将起作用。

不起作用:

<div
    style={{
    height: `200px`,
    overflow: "hidden"
    }}
>
    {this.state.items.map(item => (
    <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
    ))}
</div>
Run Code Online (Sandbox Code Playgroud)

作品:

<div>
    {this.state.items.map(item => (
    <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
    ))}
</div>
Run Code Online (Sandbox Code Playgroud)

适合不想去 codepen 的人的完整代码:

class Item extends React.Component {
  render() {
    return <div style={{ padding: `12px` }}>Item {this.props.item}</div>;
  }
}

class List extends React.Component {
  state = {
    items: [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20
    ]
  };

  componendDidMount() {
    this.state.items.forEach(item => (this[`ref_${item}`] = React.createRef()));
  }

  onClick = () => {
    scrollToComponent(this.ref_20);
  };

  render() {
    const { items } = this.state;
    return (
      <div>
        <h1>My List</h1>
        <button onClick={this.onClick}>Clickidy</button>
        {/*Replace with <div> to see it work*/}
        <div
          style={{
            height: `200px`,
            overflow: "hidden",
            backgroundColor: "red"
          }}
        >
          {this.state.items.map(item => (
            <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
          ))}
        </div>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Jja*_*nis 7

我将为您提供一个简单的解决方案,但这就是它的工作原理。您需要将包含所有元素的 div 的溢出设置为scroll。这是因为如果您将其设置为隐藏,所有无法放入该 div 的元素都会被隐藏。我给你的解决方案甚至不需要使用react-scroll-to-component或任何包。

import React from "react";
import { render } from "react-dom";

class Item extends React.Component {
  render() {
    const { item } = this.props;
    return (
      <div style={{ padding: `12px` }} id={item}>
        Item {item}
      </div>
    );
  }
}

class List extends React.Component {
  state = {
    items: [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20
    ]
  };


  onClick = () => {
    /** You can avoid using getElementById if you can get the div rendered by Item component using refs.
     * You can look at refs forwarding and other technics to see how you can solve this */
    const divToScrollTo = document.getElementById(`${this.ref_20.props.item}`);
    if (divToScrollTo) {
      divToScrollTo.scrollIntoView();
    }
  };

  render() {
    const { items } = this.state;
    return (
      <div>
        <h1>My List</h1>
        <button onClick={this.onClick}>Clickidy</button>
        <div
          style={{
            height: `200px`,
            overflow: "scroll",
            backgroundColor: "red"
          }}
        >
          {this.state.items.map(item => (
            <Item ref={inst => (this[`ref_${item}`] = inst)} item={item} />
          ))}
        </div>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

或者您可以点击下面的链接获取代码笔解决方案 https://codesandbox.io/s/2vo0j2w660

我没有进行代码中所需的更多优化,因为我时间不好,但希望这会有所帮助

如果您使用引用转发,如代码笔所示https://codesandbox.io/s/yjj66xl2v1所示,则更容易,但您必须refs在构造函数方法中创建