【问题标题】:Pass function from HOC to component (React, React native)将函数从 HOC 传递到组件(React、React native)
【发布时间】:2016-10-19 10:44:51
【问题描述】:

我是 Facebook 的 ReactJS 和 React-native 的初学者,所以我在教程的帮助下开始编码,显示 how to share code between Android and iOS.

在本教程的后面部分实现了一个按钮,用于切换状态。 不幸的是,这是用 mixin 制作的。我想用 HOC 组件来做。

原始混入

export default {
  getInitialState() {
    return {
      pressed: false
    }
  },

  handlePress() {
    this.setState({pressed: !this.state.pressed});
  }
}

上述mixin的原始调用

{ ...
  render() {
    const buttonStyle = [styles.button];
    if (this.state.pressed) buttonStyle.push(styles.buttonPress);
    return (
      <TouchableOpacity onPress={this.handlePress.bind(this)} style={buttonStyle}>
        <Text style={styles.text}>{this.props.text}</Text>
      </TouchableOpacity>
    )
  }
}

reactMixin.onClass(Button, ButtonCommon);
export default Button;

我的 HOC

export var ButtonComp = (ComposedComponent) => class extends Component {
  constructor(props) {
    super(props);
    this.state = {pressed: false};
  }

  handlePress() {
    this.setState({pressed: !this.state.pressed});
  }

  render() {
    return <ComposedComponent {...this.props} data={this.state.pressed} />;
  }
};

我的 HOC 使用情况

import { ButtonComp } from './button.common';

class Button extends Component {
  render() {
    const buttonStyle = [styles.button];
    if (this.props.data) buttonStyle.push(styles.buttonPress);

    return (
      <TouchableOpacity onPress={this.handlePress.bind(this)} style={buttonStyle}>
        <Text style={styles.text}>{this.props.text}</Text>
      </TouchableOpacity>
    )
  }
}

export default ButtonComp(Button); // Enhanced component

当我执行上面的代码时,我得到以下错误(当 this.handle 的调用发生时,所以在 TouchableOpacity 标签中):

undefined 不是一个对象(评估 'this.handlePress.bind')

那我做错了什么? HOC 是否仅用于传递数据而不是函数? 感谢您的帮助!

【问题讨论】:

    标签: javascript android ios reactjs react-native


    【解决方案1】:

    HOC 无法做到这一点。但是,如果您希望通过 this 调用包装组件中可用的 HOC 函数,则必须通过 props 传递它:

        export var ButtonComp = (ComposedComponent) => class extends Component {
            constructor(props) {
                super(props);
                this.state = {pressed: false};
                this.handlePress = this.handlePress.bind(this);
            }
    
            handlePress() {
                this.setState({pressed: !this.state.pressed});
            }
    
            render() {
                return <ComposedComponent {...this.props} handlePress={this.handlePress} data={this.state.pressed} />;
            }
        };
    
        class Button extends Component {
            render() {
                const buttonStyle = [styles.button];
                if (this.pressed) buttonStyle.push(styles.buttonPress);
    
                return (
                    <TouchableOpacity onPress={this.props.handlePress} style={buttonStyle}>
                        <Text style={styles.text}>{this.props.text}</Text>
                    </TouchableOpacity>
                )
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2019-01-18
      • 2018-08-22
      相关资源
      最近更新 更多