【发布时间】:2018-08-15 18:45:13
【问题描述】:
如何编写组件特定常量 STATUS,您可以设置 this.state.status?我认为问题在于这个上下文。如果this.STATUS 被硬编码在字符串中,它可以工作。
import { compose } from 'recompose';
import React, { Component } from 'react';
class TestComponent extends Component {
static propTypes = {};
// can you put the STATUS constant on the component?
static STATUS = {
SENT: 'SENT',
ERROR: 'ERROR',
};
state = { status: null };
// functions cannot use this.STATUS
f = () => {
this.setState({status: this.STATUS.SENT});
// undefined is not an object (evaluating '_this.STATUS.SENT')
}
render() {
const { msg } = this.statusMessage();
return ();
}
}
export default compose(
withRouter,
connect(() => ())
)(TestComponent);
【问题讨论】:
标签: javascript reactjs static