[英]How to put constants in React components, wrapped in recompose
如何編寫特定於組件的常量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);
為什么不直接將SENT
和Error
設置為狀態?
像這樣 :
this.setState({
SEND: 'SENT'
ERROR: 'ERROR'
status: null
});
然后像這樣直接設置狀態:
f = () => {
const { SENT } = this.state
this.setState({status: SENT});
}
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.