パッケージの詳細

@rooks/use-mutation-observer

imbhargav5482.5kMIT4.11.2

A React Hooks package for mutation-observer

use, react-hooks.org, react, hooks

readme

@rooks/use-mutation-observer

Note: Future updates to this package have moved to the main package rooks. All hooks now reside in a single package which you can install using

npm install rooks

or

yarn add rooks

Rooks is completely treeshakeable and if you use only 1 of the 50+ hooks in the package, only that hook will be bundled with your code. Your bundle will only contain the hooks that you need. Cheers!

TitleCard

Build Status

About

Mutation Observer hook for React.

Installation

npm install --save @rooks/use-mutation-observer

Importing the hook

import useMutationObserver from "@rooks/use-mutation-observer"

Usage

function Demo() {
  const myRef = useRef();
  const [mutationCount, setMutationCount] = useState(0);
  const incrementMutationCount = () => {
    return setMutationCount(mutationCount + 1);
  };
  useMutationObserver(myRef, incrementMutationCount);
  const [XOffset, setXOffset] = useState(0);
  const [YOffset, setYOffset] = useState(300);
  return (
    <>
      <div
        style={{
          width: 300,
          background: "lightblue",
          padding: "10px",
          position: "absolute",
          left: XOffset,
          top: YOffset
        }}
        ref={myRef}
      >
        <div
          style={{
            resize: "both",
            overflow: "auto",
            background: "white",
            color: "blue",
            maxWidth: "100%"
          }}
        >
          <p>
            Resize this div as you see fit. To demonstrate that it also updates
            on child dom nodes resize
          </p>
        </div>
        <h2>Bounds</h2>
        <p>
          <button onClick={() => setXOffset(XOffset - 5)}> Move Left </button>
          <button onClick={() => setXOffset(XOffset + 5)}> Move Right </button>
        </p>
        <p>
          <button onClick={() => setYOffset(YOffset - 5)}> Move Up </button>
          <button onClick={() => setYOffset(YOffset + 5)}> Move Down </button>
        </p>
      </div>
      <div style={{ height: 500 }} onClick={incrementMutationCount}>
        <pre>Mutation count {mutationCount}</pre>
      </div>
    </>
  );
}

render(<Demo/>)

Arguments

Argument Type Description Default value
ref React ref Ref which should be observed for Mutations undefined
callback function Function which should be invoked on mutation. It is called with the mutationList and observer undefined
config object Mutation Observer configuration {attributes: true,,characterData: true,,subtree: true,,childList: true}

更新履歴

v3.6.0-canary.0 (2019-12-25)

:rocket: New Feature

  • fullscreen, storybook

:house: Internal

  • boundingclientrect-ref, countdown, docusaurus, fork-ref, geolocation, intersection-observer-ref, isomorphic-effect, mutation-observer-ref, outside-click-ref
  • Other

:memo: Documentation

v3.5.1 (2019-12-18)

:memo: Documentation

:house: Internal

3.4.3

  • Added new useCountdown hook

3.4.2

  • Fix types for useOutsideClickRef hook
  • Internal bug fixes

v3.4.0

  • New useBoundingclientrectRef hook
  • New useForkRef hook
  • New useOutsideClickRef hook
  • New useIsomorphicEffect hook
  • New useMutationObserverRef hook
  • New useIntersectionObserverRef hook
  • Minor bug fixes

v3.3.0

  • New useGeolocation hook
  • New useThrottle hook
  • Minor bug fixes

v3.2.2

  • Fix a minor bug in useKeys

v3.2.0

  • New hooks added

    • useOnWindowResize
    • useOnWindowScroll
    • useKeys
  • Allow localStorage and sessionStorage to store all kinds of values.

v3.1

  • New hook added useDebounce

v3

  • Critical bug with useKey with refs not detected was fixed
  • useWindowSize now returns innerHeight, innerWidth, outerHeight and outerWidth. It no longer returns height and width which were ambiguous.
  • useWorker has a new signature.
  • useInterval had issues with useState which have been resolved
  • usePrevious has a much simpler implementation using useRef
  • typescript types have been added to all the hooks
  • Storybook website has been added
  • Uniform major package version for all packages. It makes development easier and it also helps tracking compatibility.

v2

  • useToggle v2.0.0 Return value is now an array with two values instead of an object

Older

  • useInterval

    -1.1.1 (2019-01-20)

  • useOutsideClick

    • Fix SSR bug introduced
    • Also look for touch listeners now
  • rooks