Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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