-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (31 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Stolen from http://ricostacruz.com/cheatsheets/umdjs.html
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['react'], function(React) { return (root.ReactLogState = factory(React)) })
} else if (typeof exports === 'object') {
root.ReactLogState = module.exports = factory(require('react'))
} else {
root.ReactLogState = factory(root.React)
}
}(window, function (React) {
var setState = React.Component.prototype.setState
function logSetState(nextState) {
var group = this.constructor.name + ' state changed'
var resolvedNextState = typeof nextState === 'function' ? nextState(this.state) : nextState
console.group(group)
console.log('Previous State', this.state)
console.log('Incoming State', resolvedNextState)
console.groupEnd(group)
return setState.apply(this, arguments)
}
var ReactLogState = function(element) {
if (element.setState === logSetState) return
element.setState = logSetState.bind(element)
console.info('Watching setState for ' + element.constructor.name)
}
ReactLogState.logAll = function() {
React.Component.prototype.setState = logSetState
console.info('Watching setState for all elements.')
}
return ReactLogState
}))