繁体   English   中英

组件有渲染方法,但没有扩展 React.Component

[英]Component has Render Method, but does Not Extend React.Component

Component appears to have a render method, but doesn't extend React.Component. This is likely to cause errors. Change <Test> to extend React.Component instead

使用extends-classes库会导致 react 组件无法渲染并出现上述错误。 我想编写一个 class ,其中包含 React.Component 方法和不同 class 中包含的一些其他方法,这样我就可以更简洁地拆分我的代码。

例子:

class Test extends classes (React.Component, OtherTestClass) {
}

有没有办法抑制错误或者我应该考虑其他一些策略?

我没想到这会奏效。 让我们看看这个extends-classes

The classes function returns a dummy class that within its constructor goes through all classes listed as arguments to the classes function and copies over all functions and properties to the aforementioned dummy class. 然后它调用所有类的构造函数。

tl; dr:它将所有内容混合在一起,因此您实际扩展的 class 不是React.Component的实际实例。 这很可能是您收到此错误的原因。

不要与语言抗争。 该语言不支持多 inheritance。 我可以建议您改用inheritance 上的合成吗?

暂无
暂无

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

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