-
Notifications
You must be signed in to change notification settings - Fork 1
MultiHash
YashTotale edited this page Dec 19, 2020
·
5 revisions
Component that pairs hashes with refs and scrolls to a corresponding ref when one of the hashes is present in the url
hashes
- Required
- An object specifying the hashes and the refs or refs with options (behavior, position, requiredPathname, scrollFunc) they correspond to
- Hashes can include or exclude leading "#"
- Applies to all hashes unless overridden by a ref with options
- Applies to all hashes unless overridden by a ref with options
- Applies to all hashes unless overridden by a ref with options
- Applies to all hashes unless overridden by a ref with options
import React from "react";
import { BrowserRouter } from "react-router-dom"; //Can use HashRouter or MemoryRouter as well
import { MultiHash } from "react-hash-scroll";
const App = () => {
const ref1 = React.createRef();
const ref2 = React.createRef();
const ref3 = React.createRef();
return (
<BrowserRouter>
<MultiHash
hashes={{
"#div": ref1,
"#heading": [ref2, { behavior: "smooth" }],
"#paragraph": [
ref3,
{ position: "start", requiredPathname: ["/docs", "/contact"] },
],
}}
requiredPathname="/docs"
/>
<div ref={ref1}>Element #1</div>
<h4 ref={ref2}>Element #2</h4>
<p ref={ref3}>Element #3</p>
</BrowserRouter>
);
};