Answer the question
In order to leave comments, you need to log in
Why did the variable declared in ComponentWillMount become Undefined?
I have a component of this type, for brevity I removed everything unnecessary.
'use strict';
class LoginFormComponent extends React.Component {
handleSubmit() {
console.log(this.model); //undefined
}
componentWillMount() {
this.model = 123;
}
render() {
console.log(this.model); //123
var styles = this.props.styles;
return (
<CM.MUI.FlatButton
style={styles.buttonStyle}
onClick={this.handleSubmit}
label={CM.gettext('Login')}/>
);
}
};
module.exports = LoginFormComponent;
Answer the question
In order to leave comments, you need to log in
Because by writing onClick={this.handleSubmit} you take the function out of context, and in this function you have this - not your component
Try writing
onClick={this.handleSubmit.bind(this)}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question