Declarative scrollIntoView component for React
npm install --save @aszabacsi/react-scrollto
Creating scroll into view animations in a declarative style using React components. You can ovverride the default scroll behaviour by creating your own custom component using the anchorWithOptions HOC.
Wrap the autoscroll target components with the base Anchor component (or create your own using the anchorWithOptions HOC).
import React, { Component } from 'react';
import { anchorWithOptions } from 'react-scrollto';
const Anchor = anchorWithOptions({
behaviour: 'smooth'
});
const setHash = (hash) => {
window.location.hash = hash;
}
class Example extends Component {
scrollTo = (hash) => {
setHash(hash);
}
render () {
return (
<div>
<button onClick={this.scrollTo('first')}>First</button>
<button onClick={this.scrollTo('second')}>Second</button>
<button onClick={this.scrollTo('third')}>Third</button>
<p>Some text here...</p>;
<Anchor anchor='first'>First</Anchor>;
<p>Some text here...</p>;
<Anchor anchor='second'>Second</Anchor>;
<p>Some text here...</p>;
<Anchor anchor='third'>Third</Anchor>;
</div>
)
}
}
MIT © aszabacsi