如何正确使用带有印第安纳拖动滚动条的 ref 来设置初始滚动位置

How to properly use ref with indiana drag scroll to set initial scroll position

我一直在尝试实现 https://codesandbox.io/s/react-indiana-drag-scroll-initial-scroll-getelement-99o6q?file=/index.js:317-382

的初始滚动

但似乎没有多少运气让 ref 工作,或者可能是渲染周期的问题。

这是我的代码,可以参考 codesandbox:

   export default function ProjectCarousel() {
  const container = useRef(null);

  useEffect(() => {
    container.current.getElement().scrollTo(0, 240);
  }, []);

  return (
    <ScrollContainer className="scroll-container" ref={container}>
      {ProjectData.map(({ id, ...otherProps }) => (
        <ProjectImage key={id} {...otherProps} />
      ))}
    </ScrollContainer>
  );
}

提前感谢您的帮助

您必须在 ref

上使用 属性 innerRef