我在客户端使用 React 来呈现我的应用程序的 View 。
当我在浏览器控制台中查看错误报告时,我有时会看到错误
检查“Constructor”的渲染方法
,而不是发生错误的类的正确名称。
例如,我会看到如下消息:
Warning: Each child in an array or iterator should have a unique "key" prop.
Check the render method of `Constructor`. See https://<fb.me>/react-warning-keys for more information.
为什么我的类(class)名称显示为 Constructor
?如何让 React 正确显示类的名称。
其他细节:
- 目前我使用
React.createClass()
创建类(即,不是 ES6 方式) - 我的一些类是使用来自 https://github.com/clayallsopp/react.backbone 的
React.createBackboneClass()
创建的促进 React 与我们遗留的 Backbone 模型/集合的交互 - 使用
gulp
和babel
编译我的 JSX 文件。
最佳答案
发生这种情况是因为您使用 createClass
创建的组件没有正确的 displayName
。将显示名称写入应用程序中的每个组件,您将看到正常的消息。
更新:
例如:
const SomeComponent = React.createClass({
displayName: 'SomeComponent',
...
render() {
...
}
});
export default SomeComponent;
关于javascript - react 模棱两可的错误消息 : "Check the render method of ` Constructor`.“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382862/