Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用react和typescript以及函数的条件实现早期函数?_Reactjs_Typescript_Function_Boolean_Early Return - Fatal编程技术网

Reactjs 如何使用react和typescript以及函数的条件实现早期函数?

Reactjs 如何使用react和typescript以及函数的条件实现早期函数?,reactjs,typescript,function,boolean,early-return,Reactjs,Typescript,Function,Boolean,Early Return,正如您所看到的,我有startAnimation函数,但我想在代码中实现一个早期返回 我需要在“try if(!this.isColorSet…”之前,首先使代码运行特性\u flag\u发光二极管 此功能\u标志\uuuuuuuuuuuuuuuuuuuuuuuuuuuuled在另一个平台上配置,其功能\u标志\uuuuuuuuuuuuuuuuuuuled为:null 因此,当特性设置为null时,startAnimation不起作用 我需要使用React中的useContext,并且特性_fl

正如您所看到的,我有startAnimation函数,但我想在代码中实现一个早期返回

我需要在“try if(!this.isColorSet…”之前,首先使代码运行特性\u flag\u发光二极管

此功能\u标志\uuuuuuuuuuuuuuuuuuuuuuuuuuuuled在另一个平台上配置,其功能\u标志\uuuuuuuuuuuuuuuuuuuled为:null

因此,当特性设置为null时,startAnimation不起作用

我需要使用React中的useContext,并且特性_flag_uLED位于文件CMSDataContext中,设置为布尔值,因为我需要使用true或false

我需要在我的代码中使用这个
{feature\u flag\uu led}=useContext(CMSDataContext)
,但我不知道在哪里

我需要做一个条件,比如:

if(!ledEnabled) {
    return SOMETHING;
}
我在哪里可以实现这两件事?谢谢

职能:

public startAnimation = async (): Promise<void> => {    
    this.logger.debug("Starting XXX lights animation");        
      try {
      if (!this.isColorSet) {
        this.logger.debug("No color SET");
        throw Error("No color set");
      }

      if (!this.isBehaviorSet) {
        this.logger.debug("No behavior SET");
        throw Error("No behavior set");
      }

      const animationColor: ILEDAnimation = {
        color: this.color,
        behavior: this.behavior,
      };
      this.logger.debug("Set Power On State XXX lights");
      await xxxController().setAnimation(animationColor);
      this.logger.debug(
        `Set Animation color: ${JSON.stringify(animationColor)}`,
      );
    } catch (error) {
      console.error("Start animation error: ", error);
      this.logger.error("Start animation error: ", error);
    }
    this.clearColorAndBehavior();
  };
publicstartanimation=async():Promise=>{
this.logger.debug(“启动XXX灯光动画”);
试一试{
如果(!this.isColorSet){
this.logger.debug(“无颜色集”);
抛出错误(“无颜色设置”);
}
如果(!this.isBehaviorSet){
this.logger.debug(“无行为集”);
抛出错误(“无行为集”);
}
常量animationColor:ILEDAnimation={
颜色:这个,
行为:这个行为,
};
this.logger.debug(“设置开机状态XXX灯”);
等待xxxController().setAnimation(animationColor);
此为.logger.debug(
`设置动画颜色:${JSON.stringify(animationColor)}`,
);
}捕获(错误){
错误(“启动动画错误:”,错误);
this.logger.error(“启动动画错误:”,错误);
}
这个.clearColorAndBehavior();
};