DEV Community

Wenchen (Neo) Li
Wenchen (Neo) Li

Posted on

React Hooks Released! (Finally πŸŽ‰)

After a lot of waiting...

We finally have React with Hooks to use! https://reactjs.org/blog/2019/02/06/react-v16.8.0.html

I also created a custom hook to use scroll position last month:

GitHub logo neo / react-use-scroll-position

A react hook to use scroll position

react-use-scroll-position

npm package

A react hook to use scroll position.

Usage

In a React functional component:

import React from 'react';
// Usually you would just need to import one of the following
import { useScrollPosition, useScrollXPosition, useScrollYPosition } from 'react-use-scroll-position';

function Example() {
  const { x, y } = useScrollPosition();
  const scrollX = useScrollXPosition();
  const scrollY = useScrollYPosition();
  return (
    <>
      <p>
        {x} should equal to {scrollX}.
      </p>
      <p>
        {y} should equal to {scrollY}.
      </p>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

In a custom React hook

import { useScrollPosition } from 'react-use-scroll-position';
function useYourImagination() {
  const { x, y } = useScrollPosition();
  return getSomethingAwesomeWith(x, y
…
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
dance2die profile image
Sung M. Kim • Edited

I can find myself using react-use-scroll-position πŸ‘

And I found that devDependencies are still not up to date.

github.com/neo/react-use-scroll-po...

I believe react should be updated to v16.8.0 (which I did for my small hook library πŸ˜‰) while TypeScript is now 3.3.x as well as the type definition .

Collapse
 
li profile image
Wenchen (Neo) Li

Very good point! Thank you v much for pointing it out!

Collapse
 
dance2die profile image
Sung M. Kim

You're welcome. I had a similar problem this morning not updating dev deps :)

Collapse
 
scriptkavi profile image
ScriptKavi

Many early birds have already started using this custom hooks library
in their ReactJs/NextJs project.

Have you started using it?

scriptkavi/hooks

PS: Don't be a late bloomer :P