javascript - 构建高阶组件并将其应用于 redux connect

标签 javascript reactjs react-native redux

请考虑 React Native 代码:

MyForm.js

Class MyForm extends Component{
   render(){
      //Code Left out for simplicity
   }
} 

function mapStateToProps(){//code left out for simplicity}

MyForm.propTypes = {//code left out for simplicity};
export default(connect(mapStateToProps, Actions)(withHocComponent(MyForm)));

HoComponent.js

export default withHocComponent = WrappedComponent => class HoComponent extends Component {
    class HocComponent extends Component{
       render(){
         <View>
           <WrappedComponent/>
         </View>
       }
    }
}

function mapStateToProps(state) {
return {
  prop1: state.myReducer.someProp,
 };
}

export default connect(mapStateToProps)(withHocComponent);

但是,我收到以下错误:

Cannot call class as a function.

堆栈指的是这一行:export default(connect(mapStateToProps, Actions)(withHocComponent(MyForm)));

除了 redux connect 函数之外,我正在尝试实现一个额外的高阶组件。

最佳答案

好的,我希望这就是您要找的。如果你有任何问题随时问。

高级组织

function withHocComponent(WrappedComponent) {
    class Wrapper extends Component {
        render() {
            // here you could pass props to your wrappedComponent
            return <WrappedComponent />;
         }

        const mapStateToProps = (state) => {
           //code left out for simplicity
        }
        //connect your HOC to the store inside the Wrapper 
        return connect(mapStateToProps, {})(Wrapper);
     }
}
export default withHocComponent;

我的表格

Class MyForm extends Component{
   render(){
      //Code Left out for simplicity
   }
} 

function mapStateToProps(){//code left out for simplicity}

MyForm.propTypes = {//code left out for simplicity};

// Here is the part where the magic happens. 
// Pass your HOC your connected component
export default withHocComponent(connect(mapStateToProps, {})(MyForm)); 

关于javascript - 构建高阶组件并将其应用于 redux connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515660/

相关文章:

javascript - 如何减少setTimeOut中 'current'的时间?

javascript - 为什么我的 TabBar 无法正确呈现在 React Native Router Flux 的顶部位置?

javascript - 使用 Javascript onclick 或 keydown 事件

javascript - 使用递归过滤 JavaScript 中的对象数据

ios - Apple LLVM 9.0 错误组 - 无法读取配置文件。找不到这样的文件或目录

reactjs - 在 Material UI 中,我如何动态地将颜色传递给我的样式?

xcode - ITMS-90725 : SDK Version Issue

react-native - React Native React 导航标题按钮事件

javascript - Angular 中指令和 Controller 之间的双向通信

reactjs - React Native - 使用图表套件时出错(执行 UI block 时抛出异常 : __NSCFNumber firstObject: unrecognized selector sent to instance)