我想知道我是否可以将一个类组件包装在一个也有一个类的 hoc 中。
import React, { Component } from "react";
import { View } from "react-native";
import { Toast } from "react-native-easy-toast";
const withToast = EnhancedComponent => {
return class HOC extends Component {
render() {
return (
<View>
<EnhancedComponent {...this.props} toast={(message, duration) => this.toast.show(message, duration)} />
<Toast
ref={toast => {
this.toast = toast;
}}
/>
</View>
);
}
};
};
export default withToast;
这是我正在使用的 hoc,现在我正在传递一个像这样的类组件:
import React, { Component } from "react";
import withToast from "../../hoc/withToast";
import Btn from "react-native-micro-animated-button";
class Login extends Component<Props> {
render() {
return <Btn title="Login" onPress={() => this.props.toast("logged in")} />;
}
}
export default withToast(Login);
当我运行它时,我收到此错误:
Invariant Violation: Invariant Violation: Invariant Violation: Element type is invalid: expected a string
(for built-in components) or a class/function (for composite components) but got: undefined.
You likely forgot to export your component from the file it's defined in, or you might have
mixed up default and named imports.
Check the render method of `HOC`
有可能吗?
谢谢!