JSX条件呈现语法错误

时间:2018-02-22 15:49:17

标签: javascript reactjs ecmascript-6 jsx

我有以下组件:

File sd = Environment.getExternalStorageDirectory();
              File sd = new File(Environment.getExternalStorageDirectory() + File.separator + "");

 //           File data = Environment.getDataDirectory();


            boolean success=true;

            if(!sd.exists())
                success=sd.mkdirs();

            if (sd.canWrite() success) {


                String backupDBPath = String.format("%s.txt", opener.getDatabaseName());
                File currentDB = mContext.getDatabasePath(opener.getDatabaseName());
                File backupDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();

            WorkMobile.getDisplayManager().showInformationLater(WorkMobile.getText("APP_BACKUP_OK_MESSAGE") );

                Toast.makeText(mContext, "Backup Successful!", Toast.LENGTH_LONG).show();

            }

        } catch (Exception e) {
            e.printStackTrace();

        }

我的linting抱怨“if”说(意外的标记)

enter image description here

2 个答案:

答案 0 :(得分:2)

括号内必须有表达式。您可以将其更改为三元组:

{(function(){
  if(props.checked)
    return <Icon name="checkmarkBlue" small />;
  //...
  return "";
})()}

或者,如果您确实需要陈述,可以使用IIFE

^\.00(?:1[5-9]\d*|[2-7]\d*|80*)$

答案 1 :(得分:1)

来自React#github

  

if-else语句不适用于JSX。这是因为JSX只是函数调用和对象构造的语法糖

您应该使用short-circuit代替。

<Layout {...props}>
    {     
      props.checked && <Icon name="checkmarkBlue" small />     
    }

或者

   render(){

      let myHtml = "";

      if(props.checked){
        myHtml = <Icon name="checkmarkBlue" small /> 
      }

      return (<Layout {...props}> { myHtml}</Layout>);
    }