【发布时间】:2021-10-29 01:29:26
【问题描述】:
我正在尝试创建滚动到元素,但出现此错误
“TypeError: 无法读取属性 'scrollIntoView' of null”。 通过控制台记录 mapRef 我可以看到我得到了正确的 div。 console.log
export class FinderComponent extends PureComponent {
constructor(props) {
super(props);
this.mapRef = React.createRef();
}
renderMap() {
return <div block="StoreFinder" ref={this.mapRef}></div>;
}
renderStoreCard(store) {
this.mapRef.current.scrollIntoView({ behavior: "smooth" });
//console.log(this.mapRef.current);
return (
<div
block="StoreFinder"
elem="Store"
key={store_name.replace(/\s/g, "")}
mods={{ isActive: store_name === selectedStoreName }}
>
{this.renderStoreCardContent(store)}
<button
block="Button"
mods={{ likeLink: true }}
onClick={() => changeStore(store)}
>
{__("Show on the map")}
</button>
</div>
);
}
}
【问题讨论】:
-
this.mapRef.current为空。
标签: javascript reactjs null js-scrollintoview create-ref