简体   繁体   English

调试反应。 确定更改了哪个道具

[英]Debug React. Identify Which prop was changed

For debugging my react app I want to know which prop was changed.为了调试我的 React 应用程序,我想知道更改了哪个道具。 When the application runs it executes componentWillReceiveProps infinite times.当应用程序运行时,它会无限次执行componentWillReceiveProps I don't know which prop was received.我不知道收到了哪个道具。 Is there any way to identify the modified props by comparing this.props and nextProps or by any other way有没有办法通过比较this.propsnextProps或通过任何其他方式来识别修改后的 props

You can use this package: maicki/why-did-you-update (about 3k stars on Github as of writing).你可以使用这个包: maicki/why-did-you-update (截至撰写本文时,Github 上大约有 3k 颗星)。

Why-did-you-update is a function that monkey patches React and notifies you in the console when potentially unnecessary re-renders occur. Why-did-you-update 是一个功能,猴子修补 React 并在发生可能不必要的重新渲染时在控制台中通知您。

I actually created a tool yesterday that helps with this problem https://www.npmjs.com/package/react-use-compare-debugger .我昨天实际上创建了一个工具来帮助解决这个问题https://www.npmjs.com/package/react-use-compare-debugger

The idea is that by including a simple useCompareDebugger("myComponent", props) it will automatically log changes to props each render.这个想法是通过包含一个简单的useCompareDebugger("myComponent", props)它将自动记录每次渲染对 props 的更改。 Within here, the key bit is it details whether values are referentially equal, which is how React does shallow comparisons to determine when it should re-render.在这里,关键是它详细说明了值是否引用相等,这就是 React 如何进行浅比较以确定何时应该重新渲染。

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM