react-mutation-observer
React wrapper for mutation observers.
Based on MutationObserver.
Getting Started
Install it via npm:
npm install --save react-mutation-observer
Install it via yarn:
yarn add react-mutation-observer
Examples
Basic usage
;
<MutationObserver onContentChange={console.log.bind(null, 'Change content triggered.')} onAttributeChange={console.log.bind(null, 'Change attribute triggered.')} onChildRemoval={console.log.bind(null, 'Child removal triggered.')} onChildAddition={console.log.bind(null, 'Child addition triggered.')}> <div className="App-intro" data-edit="EDIT ME"> REMOVE ME <b>EDIT ME</b> </div></MutationObserver>
Triggered if the the specific element is removed
;
<WatchForRemoval onRemoval={console.log.bind(null, 'Child removal triggered.')}> <div className="App-intro" data-edit="EDIT ME"> Remove the whole div </div></WatchForRemoval>
Only watch for children being added or removed
;
<WatchChildren onRemoval={console.log.bind(null, 'Child removal triggered.')} onAddtion={console.log.bind(null, 'Child addition triggered.')}> <div className="App-intro"> <b>REMOVE b tag</b> </div></WatchChildren><WatchForChildrenRemoval onRemoval={console.log.bind(null, 'Child removal triggered.')}> <div className="App-intro"> <b>REMOVE b tag</b> </div></WatchForChildrenRemoval><WatchForChildrenAddition onAddtion={console.log.bind(null, 'Child addition triggered.')}> <div className="App-intro"> <b>REMOVE b tag</b> </div></WatchForChildrenAddition>
Only watch content changes
;
<WatchContent onChange={console.log.bind(null, 'Content change triggered.')}> <div className="App-intro"> Edit Me </div></WatchContent>
Only watch attribute changes
;
<WatchAttributes onChange={console.log.bind(null, 'Attribute change triggered.')}> <div className="App-intro EditMe" data-thing="Or Edit Me"> Some Text </div></WatchAttributes>
Need More control? use the wrapper
;
return ;
Props
observedComponent: PropTypesfunc categories: PropTypesisRequired // defaults to CHILD_LIST attributeList: PropTypes subtree: PropTypesbool // defaults to false suppressAttributeOldValue: PropTypesbool // defaults to false suppressCharacterDataOldValue: PropTypesbool // defaults to false onMutation: PropTypesfuncisRequired
The different categories and mutation types are available as exports as well;
;
onMutation
{}
onMutation Payload Structures
- Type: CHILD_REMOVED || CHILD_ADDED
target previousSibling nextSibling wrappedNode child
- Type: ATTRIBUTES
from to name namespace target
- Type: CHARACTER_DATA
from to target
License
MIT