this.setState不是textfield的函数

时间:2015-10-07 17:04:47

标签: reactjs material-ui

在用户输入文本字段时,尝试在文本字段上进行验证。我想动态更改SELECT end_date - start_date AS day_diff FROM tablexxx suppose the starT_date end_date is define in the tablexxx 属性的状态。

尝试设置状态时收到错误errorText。相关代码如下。

Uncaught TypeError: this.setState is not a function

1 个答案:

答案 0 :(得分:3)

哎呀,ES6类绑定必须手动完成。

需要将.bind(this)添加到handleChange

export default class Login extends Component {
    constructor(props, context) {
        super(props, context);
        this.state = {
            username: null,
            errorCopy: null
        };
    }
    handleChange(e) {
        this.setState({errorCopy: 'Generic error copy'});
    }
    render() {
        return(
            <TextField
                hintText="Username"
                value={this.state.username}
                onChange={this.handleChange.bind(this)}
                errorText={this.state.errorCopy} />
        )
    }
}
相关问题