Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created April 18, 2017 20:33
Show Gist options
  • Save aleclarson/5cc764ee62716a98cc980a4585f28bf0 to your computer and use it in GitHub Desktop.
Save aleclarson/5cc764ee62716a98cc980a4585f28bf0 to your computer and use it in GitHub Desktop.
React debug utils
global.getParentView = do ->
ReactInstanceMap = require "ReactInstanceMap"
return (view, level = 1) ->
unless view._reactInternalInstance
view = view._currentElement._owner._instance
parent = ReactInstanceMap.get view
while parent and --level >= 0
parent = parent._currentElement._owner
if parent
then parent._instance
else null
global.getParentInstance = do ->
isNode = require "fbjs/lib/isNode"
EventPluginUtils = require "react-native/lib/EventPluginUtils"
getRenderedComponent = (inst) ->
inst._reactInternalInstance._renderedComponent
getInstanceFromHost = (host) ->
host._currentElement._owner._instance
return (inst, level = 1) ->
if isNode inst
node = EventPluginUtils.getInstanceFromNode inst
inst = getInstanceFromHost node
else
node = getRenderedComponent inst
if level is 0
return inst
parent = node
while parent and --level >= 0
parent = EventPluginUtils.getParentInstance parent
return null unless parent
parentInst = getInstanceFromHost parent
if inst is parentInst
then null
else parentInst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment