A simple higher order function to provide scroll position to a component
npm install react-provide-scroll-positionA simple higher order function to provide scroll position to a component:
1. It monitor scroll events;
1. provide scrollTop and scrollLeft as props to the wrapped components;
1. If no target DOM element selector is given, it reports scroll position of the browser window;
``
import React from 'react';
import ReactDom from 'react-dom';
import provideScrollPosition from '../../src/provideScrollPosition';
const ComponentA = ({ scrollTop, scrollLeft }) => (
const ComponentB = ({ scrollTop, scrollLeft }) => (
export const ComponentWithWindowScrollPosition = provideScrollPosition(ComponentA);
export const ComponentWithDivScrollPosition = provideScrollPosition(ComponentB, '.exampleDiv');
ReactDom.render(
`
Live demo: demo/dist/index.html
1. API: provideScrollPosition(YourComponent, [selectors]);selectors
2. is an optional string that pass to document.querySelector call;selectors` is not given, browser window scroll position is used as props;
3. if