admin管理员组文章数量:1355622
I've built a website using React (without any HTML) and I've split every page into smaller ponents. Let's say that I have for example the main page, and in the first ponent (the main section which is located at the top of the page) I have a button, and when I click on it I want it to take me to the last ponent of the page (which is located at the very bottom). I want something similar to the HTML "on click scroll to ID element".
How can I achieve that? Keep in mind that for every ponent I've created a separate js file.
Thank you!
I've built a website using React (without any HTML) and I've split every page into smaller ponents. Let's say that I have for example the main page, and in the first ponent (the main section which is located at the top of the page) I have a button, and when I click on it I want it to take me to the last ponent of the page (which is located at the very bottom). I want something similar to the HTML "on click scroll to ID element".
How can I achieve that? Keep in mind that for every ponent I've created a separate js file.
Thank you!
Share Improve this question edited Jan 27, 2022 at 7:14 Amila Senadheera 13.3k16 gold badges29 silver badges46 bronze badges asked Jan 27, 2022 at 6:40 Tomuta RazvanTomuta Razvan 511 silver badge4 bronze badges3 Answers
Reset to default 4You can achieve it using react hooks. You have to refactor the last ponent a bit, as to forward a ref
. You can expose the scroll method in the LastComp
with useImperativeHandle hook.
LastComp.js
import { useImperativeHandle, forwardRef, useRef } from "react";
const LastComp = forwardRef((props, ref) => {
const pRef = useRef();
useImperativeHandle(ref, () => ({
scrollIntoView: () => {
pRef.current.scrollIntoView({ behavior: "smooth", block: "start" });
}
}));
return (
<div style={{ height: "600px", backgroundColor: "gray" }} ref={pRef}>
Body of the last ponent
</div>
);
});
export default LastComp;
In the FirstComp you can pass the ref as refToLastComp
and call the method we exposed in the LastComp
.
FirstComp.js
const FirstComp = ({ refToLastComp }) => {
const scrolltoLast = () => {
if (refToLastComp.current) {
refToLastComp.current.scrollIntoView();
}
};
return (
<div
style={{
height: "600px",
backgroundColor: "tomato"
}}
>
<button onClick={scrolltoLast}>Scroll to Last</button>
First
</div>
);
};
export default FirstComp;
On the main page, you can create and pass a new ref to LastComp
and FirstComp
.
Main.js
import { useRef } from "react";
import FirstComp from "./FirstComp";
import SecondComp from "./SecondComp";
import LastComp from "./LastComp";
export default function Main() {
const ref = useRef(null);
return (
<>
<FirstComp refToLastComp={ref} />
<SecondComp />
<LastComp ref={ref} />
</>
);
}
It doesn't matter where the ponent is located, it can be anywhere on the home page. when you click on the button it will scroll to the ponent to view (using scrollIntoView).
Code Sandbox
Add link to the page element
<Link to="/page#elementid">Go</Link>
Do like this, in your page ponent
ponentDidMount() {
let target = window.location.hash;
target && document.querySelector(target).scrollIntoView()
}
you have to name every ponent by unique id.
let scroll_to = document.getElementById("conponent_id").offsetTop; window.scrollTo({ behavior: "smooth", top: scroll_to, });
onclick button just run this code
本文标签: javascriptHow to scroll to a specific component in React JSStack Overflow
版权声明:本文标题:javascript - How to scroll to a specific component in React JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744050475a2582330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论