javascript - 类型错误 : Failed to execute 'removeChild' on 'Node' : parameter 1 is not of type 'Node' in React

标签 javascript reactjs conditional-statements jsx

在我的渲染方法中,我调用了一个方法,它根据 API 响应数据执行一些条件逻辑。考虑到我没有从 DOM 中删除任何内容,我不清楚如何解决此错误。这是我的方法

import numeral from 'numeral';
import moment from 'moment';

class SearchResultsListItem extends React.Component {
  constructor(props) {
    this.isMaxQtyGreaterThanOneThousand=
    ::this.isMaxQtyGreaterThanOneThousand;
  }

 isMaxQtyGreaterThanOneThousand(qty){
   if(!!qty){
     if(qty%1000 === 0){
       return (numeral(qty).divide(1000).format('0,000') + 'M');
     }else{
       return numeral(qty);
     }
   }else{ return "-";}
 }


render() {
  const x = this.props.dataItem;

  return (
    <div className={`${s['quantity-block']}`}>
      {this.isMaxQtyGreaterThanOneThousand(x.MaxQuantity)}
    </div>
  )
}

为了解决这个错误,我查看了以下帖子,但没有找到任何特定于 jsx 或 React 的东西:

Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

最佳答案

该错误是因为在某些情况下,您返回的是 numeral 对象而不是辅助方法中的文本字符串。这让 React 很困惑。

你可能想要

return numeral(qty).format('0,000');

代替

return numeral(qty)

关于javascript - 类型错误 : Failed to execute 'removeChild' on 'Node' : parameter 1 is not of type 'Node' in React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45741052/

相关文章:

javascript - 如果在下拉列表中选择了某个字段,如何只运行 JQuery 函数

javascript - 物理模拟基础

javascript - 如何使用 Javascript 打开新的隐身窗口? (谷歌浏览器)

javascript - 如何以更新的状态每 x 秒调用一次函数(React)

javascript - 通过作为函数调用将 Props 传递给 React 类的正确方法

java - Java 中具有多个条件的迭代器循环

c++ - 限制 while 循环中的条件数?

javascript - 如何检查 JSON 对象数组是否包含数组中定义的值?

javascript - 灯箱效果(使用最少的 javascript)问题

android - 使用 RN 使用条件语句隐藏元素