score:4
Accepted answer
const onScroll = useCallback(event => {
const { pageYOffset, scrollY } = window;
console.log("yOffset", pageYOffset, "scrollY", scrollY);
setScrollY(window.pageYOffset);
}, []);
useEffect(() => {
//add eventlistener to window
window.addEventListener("scroll", onScroll, { passive: true });
// remove event on unmount to prevent a memory leak with the cleanup
return {
window.removeEventListener("scroll", onScroll, { passive: true });
}
}, []);
Source: stackoverflow.com
Related Query
- Get scroll position with NextJS
- Get scroll position with Reactjs
- Get scrollbar position with NextJs
- Nextjs scroll to top button, cannot get scroll position
- Get current scroll position of ScrollView in React Native
- Material UI components used with react-select resets scroll position
- Restore scroll position in React Hooks with useRef is shaky
- Page works on initial page load, but if i refresh the page I get an 404 error with Zeit, Nextjs & now
- Adding new DOM items with React in Chrome does not keep expected scroll position
- is this possible to get scroll position in material-ui select list on Scroll?
- How to get scroll position of component?
- Get screen opacity to be 0.1 at the top of scroll, but 0.95 at bottom and calculating everything in between according to the current scroll position
- Correct way to get Scroll Position
- Remove smooth scroll animation on changing page in NextJS with Link component
- Can't get Redux (Thunks) to work with NextJS using next-redux-wrapper: getInitalProps not called
- Can't get scroll position using `window.scrollY` or `window.pageYOffset`
- update scroll position every 8 seconds with useState and useEffect?
- How to detect certain scroll position with framer-motion?
- Reactjs Redux: Infinite scroll with multiple different GET requests
- How to go to another page without keeping scroll position with React
- paste token in useReducer with useContext in nextjs and get undefined
- position fixed in child element but sync parents x scroll with child elements left position
- Current scroll position updating with wrong number
- Restore scroll position after toggled a modal in react with useContext
- react-odometerjs with Nextjs dynamic imports doesn't scroll numbers
- React - How to get the scroll position of a div in JS
- How to get mouse X position of a mouse event of a div (and not of its children) with React 17?
- Make a position fixed div scroll with other contents when scroll hit certain point of page
- Getting scroll position of element ID with window.scrollY
- JS Get component position with neither GetElementById nor getBoundingClientRect
More Query from same tag
- Getting 415 error when trying to pass in multiple arguments to a get request in React with axios
- Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment -ReactJS
- How to mock async method in an interface with Jest?
- change state with prevState
- how consume webflux end point producing json streams from React
- How to trigger lottie hover when parent element is being hovered on?
- Append component dynamically into a map function
- How to apply CSS style to HTML file that is inside the public folder of React JS
- How to Post Form Data using Material-UI into API
- How testing my API calls in differents groups of test?
- Can't upload picture to MongoDb from React frontend
- HTML Table row in React component is not responding to my css styles in order to limit the height of the content
- How to add input text event handler in react
- How to loop in reactJS in table cell
- React setState callback coming before render
- How to send functional data to another page in React
- Can't access values when mapping over in next.js / react
- How to make mixins work in react es6?
- React - unable to call props value inside a usecallback function
- how to get the data from a component within a form in react
- get local image path from json in react
- A filtered list in React flashes for a split second the whole list while clicking on an item
- Where should I put general app config in a Gatsby project?
- Not able to align the material ui components from right to left
- why ReactDOM is not defined in my helloworld program?
- Purpose of React state
- Injecting into nested components with mobx-react
- How to clear <Input type="select"> selected value in Reactstrap?
- Passing component as props and using it in event
- How can I Create a webpage with a textbox and a button