我试图使用 Object.assign 采用最简单的方法:
import React, { Component } from 'react'
import { StyleSheet, View, Text, Button } from 'react-native'
class MySuperClass {
firstMethod() {
console.log("method 1")
}
}
const a = Object.assign({}, Component, MySuperClass)
class CategoriesScreen extends a {
render() {
return (
<View>
</View>
);
}
};
但是我收到错误:
TypeError: SUper expression must either be null or a function
我研究了它,但它适合与此不同的情况
如何扩展一个包含 MySuperClass 和 React 组件的对象?
(请注意:我知道我可以在 MySuperClass 中扩展 Component 作为解决方法,但我有兴趣知道如何通过这种方法实现它,谢谢)
最佳答案
你可以做到
Object.assign(MySuperClass.prototype, Component.prototype);
这是一个更完整的示例: https://codesandbox.io/s/react-component-with-objectassign-c9es0
关于javascript - 扩展 React 组件和附加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61267034/