Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created March 13, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/8ebfed3d694a670294247bdd5dbf93d2 to your computer and use it in GitHub Desktop.
Save JosePedroDias/8ebfed3d694a670294247bdd5dbf93d2 to your computer and use it in GitHub Desktop.
expose react hook internals
(function() {
var noop = function() {};
var h = __REACT_DEVTOOLS_GLOBAL_HOOK__;
var helpers = h.helpers[ Object.keys(h.helpers)[0] ];
window.helpers = helpers;
/*helpers.walkTree(
noop,
function(r) {
window.root = r;
}
);*/
window.findInstancesByName = function(name) {
var i = [];
helpers.walkTree(
function(n) {
const t = n._currentElement.type;
if (typeof t === 'function' && name === t.name) {
i.push(n._instance);
}
},
noop
);
return i;
}
window.findInstancesByTag = function(tagName) {
var i = [];
helpers.walkTree(
function(n) {
if (n._tag === tagName) {
i.push(n._currentElement._owner._instance);
}
},
noop
);
return i;
};
window.findInstancesByClass = function(className) {
var i = [];
helpers.walkTree(
function(n) {
if (n._hostNode && n._hostNode.className && n._hostNode.className === className) {
i.push(n._currentElement._owner._instance);
}
},
noop
);
return i;
};
window.findInstanceByName = function(name) {
return window.findInstancesByName(name)[0];
};
window.findInstanceByTag = function(tagName) {
return window.findInstancesByTag(tagName)[0];
};
window.findInstanceByClass = function(className) {
return window.findInstancesByClass(className)[0];
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment