【问题标题】:TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node' in ReactTypeError:无法在“节点”上执行“removeChild”:参数 1 在 React 中不是“节点”类型
【发布时间】:2018-01-26 05:16:13
【问题描述】:

在我的渲染方法内部,我正在调用一个基于 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'

【问题讨论】:

    标签: javascript reactjs conditional jsx


    【解决方案1】:

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

    你可能想要

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

    而不是

    return numeral(qty)
    

    【讨论】:

    • 我在render 方法之外执行了我的评估,如下所示: constadjustedMaxQty = numeric(x.MaxQuantity).divide(1000).format('0,000') + 'M';然后在我的渲染方法内部执行以下逻辑: {x.MaxQuantity? ((x.MaxQuantity>1000)?adjustedMaxQty: x.MaxQuantity) : "-" } 这解决了我的错误。
    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 2015-01-20
    • 1970-01-01
    • 2023-04-01
    • 2014-03-22
    • 2021-02-19
    • 1970-01-01
    相关资源
    最近更新 更多