简体   繁体   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

Using the extends-classes library causes a react component to fail rendering with the above error.使用extends-classes库会导致 react 组件无法渲染并出现上述错误。 I'd like to compose a class featuring the React.Component methods and some other methods contained in a different class so I can split my code more concisely.我想编写一个 class ,其中包含 React.Component 方法和不同 class 中包含的一些其他方法,这样我就可以更简洁地拆分我的代码。

Example:例子:

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

Is there a way to suppress the error or is there some other strategy I should consider?有没有办法抑制错误或者我应该考虑其他一些策略?

I wouldn't have expected this to work to begin with.我没想到这会奏效。 Let's examine how this extends-classes library works.让我们看看这个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. 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. It then calls the constructor of all classes.然后它调用所有类的构造函数。

tl;dr: it mashes everything together so the class you're actually extending from is not an actual instance of React.Component . tl; dr:它将所有内容混合在一起,因此您实际扩展的 class 不是React.Component的实际实例。 Which is most likely why you're getting this error.这很可能是您收到此错误的原因。

Don't fight the language.不要与语言抗争。 The language doesn't support multi inheritance.该语言不支持多 inheritance。 May I suggest you use composition over inheritance instead?我可以建议您改用inheritance 上的合成吗?

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

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