All Projects → ardaogulcan → react-use-observer

ardaogulcan / react-use-observer

Licence: MIT license
Performant react hooks for WebApi Observers, useResizeObserver, useInteractionObserver, useMutationObserver

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-use-observer

React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+14052.63%)
Mutual labels:  hooks, hook, intersection-observer
React Nprogress
⌛️ A React primitive for building slim progress bars.
Stars: ✭ 173 (+810.53%)
Mutual labels:  hooks, hook
use-scroll-direction
A simple, performant, and cross-browser hook for detecting scroll direction in your next react app.
Stars: ✭ 24 (+26.32%)
Mutual labels:  hooks, hook
resize-observer-hook
⚛️ A React Hook to monitor changes in the size of an element using native ResizeObserver API 🔍
Stars: ✭ 45 (+136.84%)
Mutual labels:  hook, resize-observer
React Universal Hooks
🎉 React Universal Hooks : just use****** everywhere (Functional or Class Component). Support React DevTools!
Stars: ✭ 148 (+678.95%)
Mutual labels:  hooks, hook
Pinst
🍺 dev only postinstall hooks (package.json)
Stars: ✭ 162 (+752.63%)
Mutual labels:  hooks, hook
Fre
👻 Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+16715.79%)
Mutual labels:  hooks, hook
Swifthook
A library to hook methods in Swift and Objective-C.
Stars: ✭ 93 (+389.47%)
Mutual labels:  hooks, hook
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (+5.26%)
Mutual labels:  hooks, hook
crypto-watchdog
Crypto Watchdog is an open-source developer friendly project, periodically queries crypto market and notifies potential pumps & recently added tokens/coins via web-hooks.
Stars: ✭ 22 (+15.79%)
Mutual labels:  hooks, hook
use-smooth-scroll
React hook which gives a smooth scrolling function.
Stars: ✭ 41 (+115.79%)
Mutual labels:  hooks, hook
Hooks
Async middleware for JavaScript and TypeScript
Stars: ✭ 117 (+515.79%)
Mutual labels:  hooks, hook
use-double-tap
React hook for handling double tap on mobile devices
Stars: ✭ 18 (-5.26%)
Mutual labels:  hooks, hook
React Use Wizard
🧙 A React wizard (stepper) builder without the hassle, powered by hooks.
Stars: ✭ 162 (+752.63%)
Mutual labels:  hooks, hook
React Resize Observer Hook
ResizeObserver + React hooks
Stars: ✭ 101 (+431.58%)
Mutual labels:  hooks, observer
Useworker
⚛️ useWorker() - A React Hook for Blocking-Free Background Tasks
Stars: ✭ 2,233 (+11652.63%)
Mutual labels:  hooks, hook
Ysf
YSF Server Functions
Stars: ✭ 77 (+305.26%)
Mutual labels:  hooks, hook
React Selector Hooks
Collection of hook-based memoized selector factories for declarations outside of render.
Stars: ✭ 84 (+342.11%)
Mutual labels:  hooks, hook
svelte-inview
A Svelte action that monitors an element enters or leaves the viewport.🔥
Stars: ✭ 358 (+1784.21%)
Mutual labels:  observer, intersection-observer
react-breakpoints
Respond to changes in a DOM element's size. With React Breakpoints, element queries are no longer "web design's unicorn" 🦄
Stars: ✭ 74 (+289.47%)
Mutual labels:  hooks, resize-observer

react-use-observer

Build Status coverage NPM JavaScript Style Guide Commitizen friendly

Performant react hooks for WebApi Observers

Features

  • Hooks for
  • Optimizes performance by re-using same Observers for same settings through the app
  • Supports three shaking

Does not include pollyfills for ResizeObserver and IntersectionObserver if you need to support older browsers please include polyfills for them.

Install

npm install react-use-observer

or

npm install --save react-use-observer

Usage

useResizeObserver: ResizeObserver

Returns

[ref: React Ref, entry: ResizeObserverEntry]

Example

import { useResizeObserver } from 'react-use-observer'

const App = () => {
  const [ref, resizeObserverEntry] = useResizeObserver()
  const { width = 0 } = resizeObserverEntry.contentRect || {}
  return (
    <div ref={ref}>
      width: {width}
    </div>
  )
}

useIntersectionObserver: IntersectionObserver

Parameters

  • options: Object - Initialization options for IntersectionObserver

    • root: An Element object which is an ancestor of the intended target, whose bounding rectangle will be considered the viewport. Any part of the target not visible in the visible area of the root is not considered visible.

    • rootMargin: A string which specifies a set of offsets to add to the root's bounding_box when calculating intersections, effectively shrinking or growing the root for calculation purposes. The syntax is approximately the same as that for the CSS margin property; see The root element and root margin in Intersection Observer API for more information on how the margin works and the syntax. The default is "0px 0px 0px 0px".

    • threshold: Either a single number or an array of numbers between 0.0 and 1.0, specifying a ratio of intersection area to total bounding box area for the observed target. A value of 0.0 means that even a single visible pixel counts as the target being visible. 1.0 means that the entire target element is visible. See Thresholds in Intersection Observer API for a more in-depth description of how thresholds are used. The default is a threshold of 0.0.

Returns

[ref: React Ref, entry: IntersectionObserverEntry]

Example

import { useIntersectionObserver } from 'react-use-observer'

const MyComp = () => {
  const [ref, intersectionObserverEntry] = useIntersectionObserver({
    root: null,
    rootMargin: '0px',
    threshold: 0.5
  })

  return (
    <div ref={ref}>
      {intersectionObserverEntry.intersectionRatio}
    </div>
  )
}

useMutationObserver: MutationObserver

Parameters

  • options: MutationObserverInit - Options to pass observe method of MutationObserver

    • attributeFilter: An array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications. No default value.

    • attributeOldValue: Set to true to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; see Monitoring attribute values in MutationObserver for details on watching for attribute changes and value recording. No default value.

    • attributes: Set to true to watch for changes to the value of attributes on the node or nodes being monitored. The default value is false.

    • characterData: Set to true to monitor the specified target node or subtree for changes to the character data contained within the node or nodes. No default value.

    • characterDataOldValue: Set to true to record the previous value of a node's text whenever the text changes on nodes being monitored. For details and an example, see Monitoring text content changes in MutationObserver. No default value.

    • childList: Set to true to monitor the target node (and, if subtree is true, its descendants) for the addition of new child nodes or removal of existing child nodes. The default is false.

    • subtree: Set to true to extend monitoring to the entire subtree of nodes rooted at target. All of the other MutationObserverInit properties are then extended to all of the nodes in the subtree instead of applying solely to the target node. The default value is false.

Returns

[ref: React Ref, entry: MutationRecord]

Example

import { useMutationObserver } from 'react-use-observer'

const MyComp = () => {
  const [ref, mutationRecord] = useMutationObserver({
    attributes: true,
    childList: true,
    subtree: true,
  })

  return (
    <div>
      Mutation Type { mutationRecord.type }
    </div>
  )
}

useObserver

Parameters

  • Observer: (Any WebApi Observer)

  • options: Object

    • observerOptions: Initialization options for given Observer
    • subscribeOptions: Options to pass observe method of Observer

Returns

[ref: React Ref, entry: ObserverEntry]

Example

import { useObserver } from 'react-use-observer'

const MyComp = () => {
  const [ref, entry] = useObserver(PerformanceObserver, {
    subscribeOptions: {
      entryTypes: ['frame'],
    },
  })

  return (
    <div>
      Entry Type { entry.entryType }
    </div>
  )
}

All WebApi Observers

License

MIT © ardaogulcan

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].