Giter Club home page Giter Club logo

rooks's Introduction




CI and Semantic Release GitHub GitHub release (latest by date) GitHub contributors npm npm bundle size Codecov branch node-lts Website Module




Essential React custom hooks ⚓ to super charge your components!



List of all hooks

🔥 Effects

🚀 Events

📝 Form

✨ Misc

🚃 Navigator

❇️ State

  • useArrayState - Array state manager hook for React
  • useCountdown - Count down to a target timestamp and call callbacks every second (or provided peried)
  • useCounter - Counter hook for React.
  • useGetIsMounted - Checks if a component is mounted or not at the time. Useful for async effects
  • useLocalstorageState - UseState but auto updates values to localStorage
  • useMapState - A react hook to manage state in a key value pair map.
  • useMultiSelectableList - A custom hook to easily select multiple values from a list
  • useNativeMapState - Manage Map() object state in React
  • usePreviousDifferent - usePreviousDifferent returns the last different value of a variable
  • usePreviousImmediate - usePreviousImmediate returns the previous value of a variable even if it was the same or different
  • usePromise - Promise management hook for react
  • useQueueState - A React hook that manages state in the form of a queue
  • useSafeSetState - set state but ignores if component has already unmounted
  • useSelect - Select values from a list easily. List selection hook for react.
  • useSelectableList - Easily select a single value from a list of values. very useful for radio buttons, select inputs etc.
  • useSessionstorageState - useState but syncs with sessionstorage
  • useSetState - Manage the state of a Set in React.
  • useStackState - A React hook that manages state in the form of a stack
  • useTimeTravelState - A hook that manages state which can undo and redo. A more powerful version of useUndoState hook.
  • useUndoRedoState - Setstate but can also undo and redo
  • useUndoState - Drop in replacement for useState hook but with undo functionality.

⚛️ UI

  • useAudio - Audio hook
  • useBoundingclientrect - getBoundingClientRect hook for React.
  • useBoundingclientrectRef - A hook that tracks the boundingclientrect of an element. It returns a callbackRef so that the element node if changed is easily tracked.
  • useFullscreen - Use full screen api for making beautiful and emersive experinces.
  • useGeolocation - A hook to provide the geolocation info on client side.
  • useInViewRef - Simple hook that monitors element enters or leave the viewport that's using Intersection Observer API.
  • useIntersectionObserverRef - A hook to register an intersection observer listener.
  • useKey - keypress, keyup and keydown event handlers as hooks for react.
  • useKeyBindings - useKeyBindings can bind multiple keys to multiple callbacks and fire the callbacks on key press.
  • useKeyRef - Very similar useKey but it returns a ref
  • useKeys - A hook which allows to setup callbacks when a combination of keys are pressed at the same time.
  • useMediaMatch - Signal whether or not a media query is currently matched.
  • useMouse - Mouse position hook for React.
  • useMutationObserver - Mutation Observer hook for React.
  • useMutationObserverRef - A hook that tracks mutations of an element. It returns a callbackRef.
  • useRaf - A continuously running requestAnimationFrame hook for React
  • useResizeObserverRef - Resize Observer hook for React.
  • useVideo - Video hook for react
  • useWindowScrollPosition - A React hook to get the scroll position of the window
  • useWindowSize - Window size hook for React.


Features

✅ Collection of 91 hooks as standalone modules.

✅ Standalone package with all the hooks at one place

✅ CommonJS, UMD and ESM Support

Installation

npm i -s rooks

Import any hook from "rooks" and start using them!

import { useDidMount } from "rooks";

Usage

function App() {
  useDidMount(() => {
    alert("mounted");
  });
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Standalone Package

Package containing all the hooks is over here. - Docs and Npm Install


License

MIT

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

Bhargav Ponnapalli
Bhargav Ponnapalli

💻 🚧
anil kumar chaudhary
anil kumar chaudhary

💻
Qiwei Yang
Qiwei Yang

💻 🚧
maciek_grzybek
maciek_grzybek

💻
Harsh Zalavadiya
Harsh Zalavadiya

💻
B V K MAHIJENDRA
B V K MAHIJENDRA

💻
Braxton Christensen
Braxton Christensen

💻
Hansel
Hansel

💻
Harshil Parmar
Harshil Parmar

💻
Lionel
Lionel

💻
Max Stoiber
Max Stoiber

💻
Michael Moore
Michael Moore

💻
Rajas Paranjpe
Rajas Paranjpe

💻
Mahendra Choudhary
Mahendra Choudhary

💻
Nghia Pham
Nghia Pham

💻
Akshay Kadam (A2K)
Akshay Kadam (A2K)

💻
Alex Golubtsov
Alex Golubtsov

💻
Arman
Arman

💻
Branden Visser
Branden Visser

💻
Brian Steere
Brian Steere

💻
Cal Courtney
Cal Courtney

💻
Chris Milson
Chris Milson

💻
Cong Zhang
Cong Zhang

💻
Daniel Holmes
Daniel Holmes

💻
Fernando Beck
Fernando Beck

💻
Josh Davenport
Josh Davenport

💻
MARCEL
MARCEL

💻
Neilor Caldeira
Neilor Caldeira

💻
Tobias Lins
Tobias Lins

💻
Tsvetan
Tsvetan

💻
Wei Zhu
Wei Zhu

💻
Yakko Majuri
Yakko Majuri

💻
Frank Hellwig
Frank Hellwig

💻
Austin Peterson
Austin Peterson

💻
thodubois
thodubois

💻
wes christiansen
wes christiansen

💻
CJ Patoilo
CJ Patoilo

💻
mar1u50
mar1u50

💻
Ayushman Gupta
Ayushman Gupta

💻
Rafael Ferreira
Rafael Ferreira

💻
Kristinn Thor Johannsson
Kristinn Thor Johannsson

💻
Michael Moore
Michael Moore

💻
Trevor Blades
Trevor Blades

💻
official_dulin
official_dulin

💻
Billy Mosis Priambodo
Billy Mosis Priambodo

💻
Stafford Williams
Stafford Williams

💻
Chanhee Kim
Chanhee Kim

💻
Hooriza
Hooriza

💻
Nils Wittler
Nils Wittler

💻
Sebastian Szczepański
Sebastian Szczepański

💻
Mahendra Choudhary
Mahendra Choudhary

💻
Som Shekhar Mukherjee
Som Shekhar Mukherjee

💻
Qiushi Pan
Qiushi Pan

💻
Jishnu Viswanath
Jishnu Viswanath

💻
brahambence
brahambence

💻
dependabot[bot]
dependabot[bot]

💻
renovate[bot]
renovate[bot]

💻
dependabot-preview[bot]
dependabot-preview[bot]

💻
github-actions[bot]
github-actions[bot]

💻
allcontributors[bot]
allcontributors[bot]

💻
zhangenming
zhangenming

💻
Antoni Kiszka
Antoni Kiszka

💻
Greg Poole
Greg Poole

💻
mergify[bot]
mergify[bot]

💻
Chaitanya J
Chaitanya J

💻
G H Mahimaanvita
G H Mahimaanvita

💻
Danilo Woznica
Danilo Woznica

💻
dan-klasson
dan-klasson

💻
Sébastien Vanvelthem
Sébastien Vanvelthem

💻
Aleksandr Soldatov
Aleksandr Soldatov

💻
Eli Yukelzon
Eli Yukelzon

💻
Mahendra Choudhary
Mahendra Choudhary

💻
Chaitanya J
Chaitanya J

💻
Dominik Dorfmeister
Dominik Dorfmeister

💻
Nghiệp
Nghiệp

💻
Seongmin Park
Seongmin Park

💻
Nate Higgins
Nate Higgins

💻

Other hooks libraries

These are some libraries that I constantly take inspiration and ideas from

  1. React-use
  2. React-aria
  3. Valtio
  4. Jotai
  5. Recoil
  6. Downshiftjs
  7. React hook form

rooks's People

Contributors

1337marcel avatar allcontributors[bot] avatar belgattitude avatar braxtonchristensen avatar chocolateloverraj avatar dependabot-preview[bot] avatar dependabot[bot] avatar elrumordelaluz avatar github-actions[bot] avatar harshilparmar avatar harshzalavadiya avatar hooriza avatar imbhargav5 avatar krijoh92 avatar maciekgrzybek avatar mahendrjy avatar mahijendra avatar mergify[bot] avatar michaelmakesgames avatar mrdulin avatar mxstbr avatar nathggns avatar neilor avatar phmngocnghia avatar qiweiii avatar qqpann avatar renovate[bot] avatar simbathesailor avatar sszczep avatar superlipbalm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rooks's Issues

Containment property not working / usage unclear

Hi guys,
I'm trying to use the containment property in the options object of the useVisibilitySensor hook, but the containment property does not seem to work (or I'm using it wrong?)

Please consider the example at: https://codesandbox.io/s/falling-hooks-orwyw?fontsize=14.

When you use your browsers' inspect element on Box 0, you'll see that the isVisible class is only removed when the Box moves out of the window viewport. However, I've passed the Container element as the containment property, so I expect the isVisible class/property to be false when the item moves out of view in the Container box. I'm not sure if I use the containment property in a wrong way or if this is a bug...

See also the following screenshot:
Screenshot 2019-05-21 at 21 47 42

The inspected .Box element has moved out of the viewport of the Container that is passed to the containment property, but still receives isVisible: true...

I'm able to simulate this problem on Google Chrome (Version 74.0.3729.157 (Official Build) (64-bit)) on Mac, but I'm also able to simulate the problem on Safari and Firefox.

Do you guys have any feedback on what is going wrong in this code? Or is this effectively a bug related to the containment property?

useInterval hooks does not work as expected in certain scenarios.

Describe the bug
So I was trying to understand hooks patterns where I tried to use useInterval hooks from rooks :). The callback is not having access to updated value. hence not updating the value in the component it uses

To Reproduce
Steps to reproduce the behavior:

  1. This is the codepen with using existing useInterval
    https://codepen.io/stack26/pen/LvRaWe?editors=1111

  2. This is the codepen where I sort of fixed it by passing dependency.
    https://codepen.io/stack26/pen/MRjPJx?editors=1011

If it's sound legit, let me know. I will complete it according to this project, adding test cases, upadating type definitions and will raise a pull request for it. This will be my first opensource contribution :).

Ideas for more hooks!

Hi Community!,

What kind of hooks do you want to see? Let's pitch some ideas here and take them up!

Single Changelog for the project.

Right now, we have multiple changelogs (one for each project). It's getting out of hand. Let's move it into one place, so it's easier for releases.

The useInterval hook creates an undesired closure over the state.

Describe the bug
The useInterval hook does not follow the recommended approach from Dan Abramov. As a result, it maintains the closure over outside objects used in the callback function. For example, using the state returned by useReducer in the useInterval callback will result in the callback function never seeing any updated state.

To Reproduce
Steps to reproduce the behavior:

  1. Call useReducer to create a state.
  2. Call useInterval to call an increment action on the state (via the reducer).
  3. Print out the state in the useInterval callback function.
  4. Observe that the logged state never changes.

Expected behavior
That the state seen in the useInterval callback should reflect the current state.

MutableRefObject<HTMLElement> in useBoundingClientRect

Thanks for developing this library! I'm just diving into hooks but this seems like a great resource.

Describe the bug
Currently the useBoundingClientRect hook expects a MutableRefObject<HTMLElement>.

Based on the behavior and typings for useRef from the DefinitelyTyped definitions for React, producing a MutableRefObject<HTMLElement> does not appear to be possible.

The closest I can get is a MutableRefObject<HTMLElement | null> element which I think should be the expected input type.

To Reproduce
The following code will reproduce the behavior:

const ref = useRef<HTMLElement | null>(null);
const boundingclientrect = useBoundingClientRect(ref);

Error Produced:

Argument of type 'MutableRefObject<HTMLElement | null>' is not assignable to parameter of type 'MutableRefObject<HTMLElement>'.
  Type 'HTMLElement | null' is not assignable to type 'HTMLElement'.
    Type 'null' is not assignable to type 'HTMLElement'.ts(2345)

Using the following library versions:

Additional context
See the following issue on DefinitelyTyped for an in-depth discussion of the various typings for the output of useRef and why it appears MutableRefObject<HTMLElement | null> is the correct typing for the use-case.

DefinitelyTyped/DefinitelyTyped#31065

Note: It's easy to work around this using any but I thought I'd open an issue because having accurate typings is always great!

Travis.ci continuous integration

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

JSDoc style comments

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Return array instead of object for useInterval hook

Is your feature request related to a problem? Please describe.
I would like to be able to give my own names for start and stop functions returned by useInterval hook. It is possible to use object renaming like { start: myStartFunc } with current implementation, however it would be more inline with react hooks to have them returned as array for easier renaming.

Describe the solution you'd like
Return start, stop, intervalId in array instead of in object.

Describe alternatives you've considered
See above

Additional context
N/A

Re-exporting other hooks

I had a similar idea to release a hooks library. I'd have some hooks also on my own that I want to release and it would be nice if I'd be able simply include them in rooks. Would you consider such solution (re-exporting 3rd party hooks)? There are definitely some nice hooks out there and I think it would make more sense reusing those than recreating the same.

Improve useKey and useKeys

Describe the feature
useKey and useKeys support different kinds of key identifiers. We need to make them uniform and make sure we support all use cases.

useKeys hooks for listening to one or multiple keypresses

Feature

useKeys hooks allow adding certain action when one or multiple keys are pressed at the same time.

I have picked up the task of creating this hook.

I will be using this ticket for the questions and track the progress of this specific hook.

Add a way to update useBoundingClientRect with window's resize and scroll events

ClientRects are tending to be updated with resizing or scrolling the window. There is no way to capture these updates in the current implementation.

There are two solutions that come into my mind:

  1. Add options to useBoundingClientRect, e.g. { updateWithWindowScroll: boolean, updateWithWindowResive: boolean }, as the second argument, and handle these events inside useBoundingClientRect.

    Usage:

    useBoundingClientRect(ref, { updateWithWindowScroll: true })
  2. Return a update function from useBoundingClientRect. Calling update() will trigger updating the ClientRect.

    Usage:

    const { update } = useBoundingClientRect(ref)

I am willing to create a pull request if one of the solutions sounds good to you :)

Create new hook - useThrottle

There's already hook called useDebounce, who serves similar purposes, but we need to add a new one that will handle function throttling. These concepts might sound very similar but they're actually quite different. Good explanation here.

I'm more than happy to work on this ticket.

useLazyImage

Is your feature request related to a problem? Please describe.
We need a hook to lazily load imgs

Describe the solution you'd like

function Profile(){
   const [src, isLoading, isVisible] = useLazyImage(src, fallbackSrc, visibilityOptions)
   return <img src={src} />
}

It should internally use @rooks/use-visibility-sensor

Describe alternatives you've considered
We have been using react-lazy-image and react-lazy-progressive-image and it's about time we have a hook!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.