reactjs object doesn't have key __reactInternalInstance

[亡魂溺海] 提交于 2019-12-13 00:45:58

问题


I copied this function from another stackoverflow subject (to call child methods outside render) and it worked in a small test project

window.FindReact = function(dom) {
  for (var key in dom) {
    if (key.startsWith("__reactInternalInstance$")) {
        var compInternals = dom[key]._currentElement;
        var compWrapper = compInternals._owner;
        var comp = compWrapper._instance;
        return comp;
    }
  }
  return null;
};

I made a class that shows a messagebox in mid of screen (Fade-in; show msg few seconds; then fade out)

and I could call the method to show the message of the child component directly thanks to above function. So all good.

Then I copied that object (messagebox) to my main project and copied above function to the parent object. And when I call it, no key ... well not the key I'm looking for

i listed the keys both of the working and failing project. all keys are the same except the first one (which is missing in the fail project)

key=__reactInternalInstance$bv23g7yipka

The fail project works, no errors, also the message component works (can call from inside to show message)

I really have no idea why this fails (key is missing), hope some of you do.


回答1:


i checked the package.js and noticed the different versions of all modules. So updated them (react/react-dom/webpack/babel-loader) and then it worked :)



来源:https://stackoverflow.com/questions/44829051/reactjs-object-doesnt-have-key-reactinternalinstance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!