reactjs 对象没有密钥 __reactInternalInstance [英] reactjs object doesn't have key __reactInternalInstance

查看:225
本文介绍了reactjs 对象没有密钥 __reactInternalInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个 stackoverflow 主题复制了这个函数(在渲染之外调用子方法)并且它在一个小型测试项目中工作

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;
};

我做了一个在屏幕中间显示消息框的类(淡入;显示 msg 几秒钟;然后淡出)

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

fail 项目有效,没有错误,消息组件也有效(可以从内部调用以显示消息)

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.

推荐答案

我检查了 package.js 并注意到所有模块的不同版本.所以更新了它们(react/react-dom/webpack/babel-loader)然后它起作用了:)

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 :)

这篇关于reactjs 对象没有密钥 __reactInternalInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆