简体   繁体   English

React:如何调试为什么浏览器在Chrome> Dev Tools中显示的值不同于HTML元素?

[英]React: How to debug why browser showing different value than HTML element in Chrome > Dev Tools?

I am building an app in react. 我正在构建一个反应应用程序。

Problem 问题

My app in Chrome browser is showing a different value than the corresponding HTML element in Dev Tools. 我在Chrome浏览器中的应用显示的值与开发工具中相应的HTML元素不同。 This only occurs after a user action on the UI to sort the table. 这仅在用户对UI进行表格排序操作之后发生。 It seems the browser is still showing the old value for that row in the table. 看来浏览器仍在表中显示该行的旧值。

  1. Browser showing value = 3 <-- incorrect 浏览器显示值= 3 <-错误
  2. HTML element has value = 41 <-- correct HTML元素的值= 41 <-正确

See image below: 见下图:

在此处输入图片说明

Question

What steps should I take to diagnose what may be causing this issue? 我应该采取什么步骤来诊断可能导致此问题的原因?

eg is there somewhere I should add specific logs? 例如,我应该在哪里添加特定日志? or is this a known issue in react when someone is doing something clearly wrong? 还是当某人明显做错了事情时,这是一个已知的反应问题?

Note: This request is more about the how to diagnose such an issue, rather than finding what is specifically wrong with my code. 注意:此请求更多地是关于如何诊断这样的问题,而不是查找我的代码有什么特别错误。

The problem was in the code it seems. 问题 ,似乎代码。 I found the cause through trial and error changing the code. 我通过反复尝试更改代码找到了原因。

Previously , the render for my component looked like: 以前 ,我组件的渲染看起来像:

 render() {
    return (
      <td className="hoverWrapper">
          <input
            className="wrappedContent"
            type="number"
            onBlur={this.inputChangeHandler}
            value={this.props.value}
          ></input>
          <EditOnhoverbutton />
      </td>
    )
  }

Changed to: 变成:

 render() {
    const showValue = this.props.value ? this.props.value : "";
    return (
      <td className="hoverWrapper">
          <input
            className="wrappedContent"
            type="number"
            onBlur={this.inputChangeHandler}
            value={showValue}
          ></input>
          <EditOnhoverbutton />
      </td>
    )
  }

I suspect the underlying cause was related to the fact that for some rows this.props.value did not exist, and trying to render undefined caused the render to fail, and the original value continued to display. 我怀疑根本原因与以下事实有关:对于某些行, this.props.value不存在,并且尝试渲染undefined导致渲染失败,并且原始值会继续显示。

If anyone can provide a better assessment, I would be grateful. 如果有人能提供更好的评​​估,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Chrome开发者工具报告的字体大小与CSS不同 - Why chrome dev tools reports font size different than css 为什么会根据您在浏览器开发工具中的选择方式为同一元素显示不同的选择器? - Why are different selectors shown for the same element based on how you select it in browser dev tools? 为什么Chrome开发者工具会显示未绑定到我的元素的单击事件监听器? - Why is Chrome dev tools showing click Event Listeners that aren't bound to my element? 如何隐藏chrome dev工具中的html注释? - How to hide html comments in chrome dev tools? Chrome 开发工具:检查器显示错误的元素大小? - Chrome Dev Tools: Inspector showing wrong element size? 像 JavaScript 中的 Chrome Dev Tools 一样突出显示 HTML 元素 - Highlight HTML element just like the Chrome Dev Tools in Javascript 元素存在于Chrome开发工具中,但不存在于View Source中-这怎么可能? - Element present in Chrome Dev Tools but NOT in View Source - how is this possible? 如何使用浏览器 Web 开发工具仅搜索 HTML 元素? - How to search for only HTML elements using browser web dev tools? Chrome 开发工具导出元素 HTML - Chrome Dev Tools export Elements HTML Internet Explorer显示与开发工具中计算出的颜色不同的颜色字体 - Internet explorer showing different color font than it's computed color in dev tools
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM