Javascript 在react中布线后滚动到视图
如何在react中路由后滚动到视图Javascript 在react中布线后滚动到视图,javascript,reactjs,react-router,Javascript,Reactjs,React Router,如何在react中路由后滚动到视图 我们正在使用react路由器。我想要实现的是在返回到某个组件页面后,在其中一个组件上进行滚动查看。滚动到组件的一种方法是在要滚动到的组件上放置一个参考: 将ref放到组件上后,可以滚动到父组件的componentDidMount()中的ref,如: window.scrollTo(0,this.myRef.current.offsetTop) 在这里,您可能需要稍微加强防御,并执行以下操作: this.myRef&&window.scrollTo(0,thi
我们正在使用react路由器。我想要实现的是在返回到某个组件页面后,在其中一个组件上进行滚动查看。滚动到组件的一种方法是在要滚动到的组件上放置一个参考: 将ref放到组件上后,可以滚动到父组件的
componentDidMount()
中的ref,如:
window.scrollTo(0,this.myRef.current.offsetTop)
在这里,您可能需要稍微加强防御,并执行以下操作:
this.myRef&&window.scrollTo(0,this.myRef.current.this.myRef)
这样,当访问路由时,组件将滚动到其
偏移滚动到组件的一种方法是在要滚动到的组件上放置一个参考:
将ref放到组件上后,可以滚动到父组件的componentDidMount()
中的ref,如:
window.scrollTo(0,this.myRef.current.offsetTop)
在这里,您可能需要稍微加强防御,并执行以下操作:
this.myRef&&window.scrollTo(0,this.myRef.current.this.myRef)
这样,当访问路由时,组件将滚动到其偏移位置
下面是一个使用react router dom
和refs
的快速示例。你没有提供任何代码给我们看,所以考虑这个模板。p>
这里还有一个沙箱供您参考:
假设您的路线设置如下:
路线
他们点击链接
,然后进入/example
路线,呈现example
import React from "react";
import Section from "./Section";
class Example extends React.Component {
componentDidMount() {
if (this.mySection.current) {
this.mySection.current.scrollIntoView({
behavior: "smooth",
nearest: "block"
});
}
}
mySection = React.createRef();
render() {
return (
<div>
<div style={{ background: "orange", height: "750px" }}>
This is an example, below is my component.
</div>
<div ref={this.mySection}>
<Section />
</div>
</div>
);
}
}
export default Example;
从“React”导入React;
从“/Section”导入节;
类示例扩展了React.Component{
componentDidMount(){
if(this.mySection.current){
this.mySection.current.scrollIntoView({
行为:“平滑”,
最近的:“街区”
});
}
}
mySection=React.createRef();
render(){
返回(
这是一个示例,下面是我的组件。
);
}
}
导出默认示例;
该示例包含两部分,一个包含纯文本的div和部分
组件。正如您所注意到的,我们将部分
组件包装在一个div中,并给它一个ref
prop。ref
允许我们与该包装器div通信。在componentDidMount()
中,我们只需滚动到该div。这里有一个使用react-router dom
和refs
的快速示例。你没有提供任何代码给我们看,所以考虑这个模板。p>
这里还有一个沙箱供您参考:
假设您的路线设置如下:
路线
他们点击链接
,然后进入/example
路线,呈现example
import React from "react";
import Section from "./Section";
class Example extends React.Component {
componentDidMount() {
if (this.mySection.current) {
this.mySection.current.scrollIntoView({
behavior: "smooth",
nearest: "block"
});
}
}
mySection = React.createRef();
render() {
return (
<div>
<div style={{ background: "orange", height: "750px" }}>
This is an example, below is my component.
</div>
<div ref={this.mySection}>
<Section />
</div>
</div>
);
}
}
export default Example;
从“React”导入React;
从“/Section”导入节;
类示例扩展了React.Component{
componentDidMount(){
if(this.mySection.current){
this.mySection.current.scrollIntoView({
行为:“平滑”,
最近的:“街区”
});
}
}
mySection=React.createRef();
render(){
返回(
这是一个示例,下面是我的组件。
);
}
}
导出默认示例;
该示例包含两部分,一个包含纯文本的div和部分
组件。正如您所注意到的,我们将部分
组件包装在一个div中,并给它一个ref
prop。ref
允许我们与该包装器div通信。在componentDidMount()
中,我们只需滚动到该div即可。@ODelibalta谢谢!嗨@JayShi,我刚刚写了一篇文章,为你提供了一个快速而清晰的例子,说明你是如何做到这一点的。如果有帮助,请告诉我。@ODelibalta谢谢!嗨@JayShi,我刚刚写了一篇文章,为你提供了一个快速而清晰的例子,说明你是如何做到这一点的。让我知道这是否有帮助。只是想知道为什么这被否决了?任何关于为什么会很棒的反馈,因为它工作得很好,只是想知道为什么这被否决了?任何关于为什么会很好的反馈,因为它工作得很好,它总是滚动。如果是路线,则不需要。当使用链接时,它是必需的。你能更新设置吗?它总是滚动的。如果是路线,则不需要。当使用链接时,它是必需的。你能更新设置吗?
import React from "react";
import Section from "./Section";
class Example extends React.Component {
componentDidMount() {
if (this.mySection.current) {
this.mySection.current.scrollIntoView({
behavior: "smooth",
nearest: "block"
});
}
}
mySection = React.createRef();
render() {
return (
<div>
<div style={{ background: "orange", height: "750px" }}>
This is an example, below is my component.
</div>
<div ref={this.mySection}>
<Section />
</div>
</div>
);
}
}
export default Example;