如何在高阶组件中包装类?

时间:2019-09-24 15:36:25

标签: javascript reactjs typescript react-native react-hoc

我想知道是否可以将类组件包装在还具有类的专案中。

Tree

这是我正在使用的临时工具,现在我正在传递这样的类组件:

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;

当我运行它时,出现此错误:

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);

有可能吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

弄清楚了。 我正在导入 import { Toast } from "react-native-easy-toast"; 代替 import Toast from "react-native-easy-toast";