最新消息:20210917 已从crifan.com换到crifan.org

【已解决】ReactJS中如何在初始化时定义常量和引用常量

ReactJS crifan 4048浏览 0评论

想要在ReactJS中定义和引用常量,但是提示未定义:

export default class BreedingProcess extends Component {
  InitialInvalidValue : {
    isBreeding : "-1",
    //Not Breeding
    notBreedingReason : "0",
    //Is Breeding
    breedingCowId : "",
    semenCount : null,
    isSexControl : "-1",
    breeder : "-1"
  };
  state = {
    breedInfoDict : {
      cow_code : "",
      cowshed_id : "",
      cur_alarm_time : "",
      prev_alarm_time : ""
    },
    isBreeding : InitialInvalidValue.isBreeding,
    saveBtnDisabled : true,

[eslint] ‘InitialInvalidValue’ is not defined. (no-undef)

reactjs initial const

How to define constants in ReactJS – Stack Overflow

用statics结果语法错误。

【总结】

最后是用:

在ReactJS的Class之外,定义了const初始化常量,而解决了问题:

const InitialInvalid = {
  isBreeding : "-1",
  //Not Breeding
  notBreedingReason : "0",
  //Is Breeding
  breedingCowId : "",
  semenCount : null,
  isSexControl : "-1",
  breeder : "-1"
};
@connect(reducer, bindActions(actions))
export default class BreedingProcess extends Component {
  state = {
    breedInfoDict : {
      cow_code : "",
      cowshed_id : "",
      cur_alarm_time : "",
      prev_alarm_time : ""
    },
    isBreeding : InitialInvalid.isBreeding,
    saveBtnDisabled : true,
    //Not Breeding
    notBreedingReason : InitialInvalid.notBreedingReason,
    //Is Breeding
    breedingCowId : InitialInvalid.breedingCowId,
    semenCount : InitialInvalid.semenCount,

解决了问题。

转载请注明:在路上 » 【已解决】ReactJS中如何在初始化时定义常量和引用常量

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.184 seconds, using 20.13MB memory