npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-mutation-observer

v1.0.0

Published

Performant react hook for WebApi MutationObserver

Downloads

128

Readme

React hook use-mutation-observer

Performant react hook for WebApi Mutation Observer

Installation

npm install react-use-mutation-observer

or

yarn add react-use-mutation-observer

Example without value

import React, { useState, useRef } from 'react'
import useMutationObserver from 'react-use-mutation-observer'

const App = () => {
  const [target, setTarget] = useRef()
  const [changes, setChanges] = useState()

  useMutationObserver(target.current, (mutations, observer) => {
    const changes = mutations.map((mutation) => {
      return {
        type: mutation.type,
        target: mutation.target,
        attributeName: mutation.attributeName,
        oldValue: mutation.oldValue
      }
    })
    return setChanges(changes)
  })

  return (
    <div>
      <div ref={target}>Target</div>
      <div>Changes: {JSON.stringify(changes)}</div>
    </div>
  )
}

Example with value

import React, { useState, useRef } from 'react'
import useMutationObserver from 'react-use-mutation-observer'

const App = () => {
  const [target, setTarget] = useRef()
  const [value, observer] = useMutationObserver(target.current, (mutation) => {
    const { type, target, attributeName, oldValue } = mutation
    return target.getAttribute(attributeName)
  })
}

return (
  <div>
    <div ref={target}>Target</div>
    <div>Changes: {JSON.stringify(value)}</div>
  </div>
)

API

This package exposes one function, useMutationObserver, which runs a callback whenever a target element changes, passed in as a react ref.

By default, only changes to the target element attributes fire the callback. You can opt to pass in options configuration the third argument as needed. These options are the same as the MutationObserverInit interface.

type MutationObserverOptions = {
  attributes?: boolean
  attributeOldValue?: boolean
  attributeFilter?: string[]
  characterData?: boolean
  characterDataOldValue?: boolean
  childList?: boolean
  subtree?: boolean
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  [key: string]: any
}
  • 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

[value, observer]

License

MIT © prma85