无法读取null的属性'getHostNode'

问题描述 投票:35回答:11

我有一个视觉/反应应用程序与反应路由器,我有一个简单的按钮在我的应用程序:

<Link className="dark button" to="/">Another Search</Link>

当我点击它时,我得到以下异常:

Uncaught TypeError: Cannot read property 'getHostNode' of null

错误来自:

getHostNode: function (internalInstance) {
    return internalInstance.getHostNode();
},

知道为什么我得到这个?

javascript reactjs react-router
11个回答
18
投票

我面临着类似的问题。事实证明,在我的情况下,突出显示从生成的dom中删除注释。

对于文本,React 15正在使用reactid而不是span标记添加注释,如:

<!-- react-text: 248-->
Another Search
<!--/react-test-->

你能尝试这样的东西吗?

<Link className="dark button" to="/"><span>Another Search</span></Link>

这将强制生成的DOM包含具有正确data-reactid属性的span。

我会向react-router提出一个问题,也许他们可以在内部做到这一点,所以你不必为此烦恼。但是,由于Link儿童基本上可以做任何事情,因此存在挑战。


0
投票

在我的情况下,React不在文件的范围内。

如果从不同的文件导入具有jsx的变量,该文件中没有导入反应。

import React from "react";

使用以下eslint插件可以避免这种情况:react-in-jsx-scope

资料来源:https://github.com/yannickcr/eslint-plugin-react/issues/84


-1
投票

我的解决方案是在使用Chrome时删除缓存的图像,文件和Cookie。设置 - >隐私和安全 - >清除浏览数据 - >缓存的图像和文件/ Cookies和其他网站数据


11
投票

我在最近几天多次遇到这个问题(新的反应),几乎在所有情况下,都有一些语法/代码错误没有被其他地方捕获。一个例子:如果你写:

getInitialState() {
    showModal: false
},

代替:

getInitialState() {
    return {
        showModal: false
    };
},

你会收到这个错误。这是除非您的构建过程尚未捕获错误。希望这可以帮助某人(或者我自己几天.Hi Niyaz, you are welcome!)。


2
投票

对我来说,这是一个错字,导致从错误的模块导入组件。

import { Link, Icon } from 'react-router';
import { Tag } from 'antd';

它应该是

import { Link } from 'react-router';
import { Tag, Icon } from 'antd';

2
投票

我只需要重启我的nodemon后端。


2
投票

如果有人找到这个帖子。对我来说,这对于道具来说是一个零错误。

代码生成错误:

<Menu InventoryCount={this.state.inventoryModel.length} />

使用null检查代码:

<Menu InventoryCount={this.state.inventoryModel ? this.state.inventoryModel.length : 0} />

1
投票

非常有趣:)对我来说,事实证明我在儿童组件中错误地消费了道具。可能对某人有帮助。

function Parent(){
    const styleEle = { width: '100px'};
    return (<div>
            <Child styleO={styleEle}/>
        </div>);
}

function Parent(props){
    // here i was directly using <div style={styleO}> causing issue for me
    return (<div style={props.styleO}>
            {'Something'}
        </div>)
}

1
投票

如果你得到像null的“getHostNode”这样的错误,那么它的错误与之前编写的旧代码相关,并且它带有版本更新的反应

我们有两种方法来解决相同问题1)首先我们必须从项目中卸载react,然后再次安装与指定版本的反应(旧的15.4.2)当前版本的反应是15.6.1 2)第二种方式是耗时的但是对于应用程序的未来它的好处,通过旧的代码和处理错误(承诺的错误处理)以正确的方式跟随几个链接,帮助你弄清楚什么是后面运行

https://github.com/facebook/react/issues/8579 https://github.com/mitodl/micromasters/pull/3022


1
投票

我试图错误地渲染undefined值时遇到此错误。

render(){
  let name = this.getName(); // this returns `undefined`
  return <div>name: {name}</div>
}

修复是回退到null(其中是可接受的值)

render(){
  let name = this.getName(); // this returns `undefined`
  return <div>name: {name || null}</div>
}

0
投票

我有类似的问题。

我试图从node_modules手动更新一些插件,当我还原它时,我得到了这个错误。

我通过删除node_modules并运行NPM安装来解决它。

© www.soinside.com 2019 - 2024. All rights reserved.