高阶函数和不同文件中的类

时间:2017-08-31 18:23:14

标签: reactjs react-native ecmascript-6 higher-order-functions

目前使用react-native并使用High Order函数。我有一些我正在使用的演示组件,我目前有一个处理一些布局属性的HOC容器。

我现在意识到我想拥有多个容器,这些容器将是同一个类的不同配置。为此,我在一个不同的文件中创建了一个类,但问题是我似乎无法通过箭头函数将我的组件传递给类。我很确定我错过了一些非常微不足道的东西。

以下是了解问题的部分代码:

BaseContainer:

export default class BaseContainer extends Component {

   render(){
      <Wrapped/>
   }

}

HOC组件(忽略2个导出默认值,这些是2个不同的模块):

export default RegularContainer = (Wrapped) => BaseContainer;

export default MessageContainer = (Wrapped) => class extends BaseContainer {

    constructor(props){
        super(props);
        this._borderStyle = 'containerLeftBorder';                
    }

}   

我得到的错误是&#34;无法找到变量:Wrapped&#34;在BaseContainer中,这是可以理解的但我无法弄清楚当类在另一个模块中时如何传递Wrapped变量。

如果我在同一个文件中定义BaseContainer类的内容,这工作正常。

1 个答案:

答案 0 :(得分:0)

嗯,我“解决了”但它看起来有点难看。我很乐意听到一些反馈。否则我会以答案结束:

export default RegularContainer = (Component) => class extends BaseContainer {

        constructor(props){
            super(props);
            this._Wrapped = Component;             
        }

    }  



export default MessageContainer = (Component) => class extends BaseContainer {

    constructor(props){
        super(props);
        this._borderStyle = 'containerLeftBorder';   
        this._Wrapped = Component;             
    }

}