admin管理员组文章数量:1313739
I am a beginner regarding Facebooks ReactJS and React-native, so I started coding with help of a tutorial, showing how to share code between Android and iOS.
Later in this tutorial a button is implemented, which toggles a state. Unfortunately this is made with a mixin. I want to do it with an HOC-ponent.
Original mixin
export default {
getInitialState() {
return {
pressed: false
}
},
handlePress() {
this.setState({pressed: !this.state.pressed});
}
}
Original call of above 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;
My 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} />;
}
};
My HOC usage
import { ButtonComp } from './buttonmon';
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 ponent
When I execute above code, I get the following error (when the call of this.handle happens, so in the TouchableOpacity tag):
undefined is not an object (evaluating 'this.handlePress.bind')
So what am I doing wrong? Are HOC only to pass data, not functions? Thanks for the help!
I am a beginner regarding Facebooks ReactJS and React-native, so I started coding with help of a tutorial, showing how to share code between Android and iOS.
Later in this tutorial a button is implemented, which toggles a state. Unfortunately this is made with a mixin. I want to do it with an HOC-ponent.
Original mixin
export default {
getInitialState() {
return {
pressed: false
}
},
handlePress() {
this.setState({pressed: !this.state.pressed});
}
}
Original call of above 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;
My 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} />;
}
};
My HOC usage
import { ButtonComp } from './button.mon';
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 ponent
When I execute above code, I get the following error (when the call of this.handle happens, so in the TouchableOpacity tag):
undefined is not an object (evaluating 'this.handlePress.bind')
So what am I doing wrong? Are HOC only to pass data, not functions? Thanks for the help!
Share Improve this question edited Oct 21, 2016 at 6:09 bytepool asked Oct 19, 2016 at 10:44 bytepoolbytepool 851 silver badge6 bronze badges1 Answer
Reset to default 9HOC cannot do that. Yet, in case you want a function from HOC available in the wrapped ponent to be called there through this
, you must pass it through 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>
)
}
}
本文标签: javascriptPass function from HOC to component (ReactReact native)Stack Overflow
版权声明:本文标题:javascript - Pass function from HOC to component (React, React native) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741958945a2407155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论