首页 > 解决方案 > 如何在 antd 步骤中滚动到顶部。反应

问题描述

我正在使用antd. 我希望每当用户进入下一步或上一步时,页面应该滚动到顶部。我使用 window.scrollTo(0, 0) 和 window.top=0; 但它不起作用。谁能帮助我如何滚动到顶部。

previousStep = () => {
    window.scrollTo(0, 0);
    window.scrollTop = 0;
    const { currentStep } = this.state;
    this.setState({ currentStep: currentStep - 1 });
};

标签: javascriptreactjsantd

解决方案


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

componentDidMount() {
  this.myRef.current.scrollTo(0, 0);
}

render() {
    return <div ref={this.myRef}></div> 
}   // attach the ref property to a dom element

推荐阅读