NodeJS 有没有什么方法可以根据react js中的函数返回类型来显示和隐藏react中的按钮

whhtz7ly  于 2022-12-29  发布在  Node.js
关注(0)|答案(2)|浏览(120)

有没有什么方法可以根据react js中的函数返回类型来显示和隐藏react中的按钮
datecheck是返回的函数,如果当前日期和结束时间相同,则不会在返回区域调用该函数,有人能指出哪里出错了吗
这是我的职责

const datecheck = async () => {
    const current = moment().format('DD/MM/YYYY');
    const endDate=getDateFormat(endTime)
    if(current == endDate){
      console.log("11111111111---------->")
      return true;
    }else{
      console.log("2222222222---------->")
      return false;
    }
  };

      //This is where i am trying to access
      {
        datecheck === true ?
        <Grid container style={{ justifyContent: 'center', paddingBottom: 10 }}>
        <Grid item className={''} />
        <Grid item>
          <Typography>
            {user && user.Type === KEY_USER_TYPE.faculty ? (
              <Button
                variant="contained"
                target="blank"
                color="primary"
                onClick={onStartClass}
              >
                {t('labels:Start_clas')}
              </Button>
            ) : user && user.Type === KEY_USER_TYPE.student ? (
              <Button
                variant="contained"
                target="blank"
                color="primary"
                onClick={onJoinClass}
              >
                {t('labels:Join_Class')}
              </Button>
            ) : null}
          </Typography>
        </Grid>
      </Grid>:""
      }
    
    </Box>
  );
vtwuwzda

vtwuwzda1#

您将使用“===”运算符将函数本身与布尔值true进行比较,而布尔值true将始终为false

{
  datecheck() === true ?
  <Grid container style={{ justifyContent: 'center', paddingBottom: 10 }}>
  <Grid item className={''} />
  <Grid item>
    <Typography>
      {user && user.Type === KEY_USER_TYPE.faculty ? (
        <Button
          variant="contained"
          target="blank"
          color="primary"
          onClick={onStartClass}
        >
          {t('labels:Start_clas')}
        </Button>
      ) : user && user.Type === KEY_USER_TYPE.student ? (
        <Button
          variant="contained"
          target="blank"
          color="primary"
          onClick={onJoinClass}
        >
          {t('labels:Join_Class')}
        </Button>
      ) : null}
    </Typography>
  </Grid>
</Grid>:""
}
n1bvdmb6

n1bvdmb62#

这是一个你可以让它工作的方法:
1.删除'async'
1.包括括号-datecheck()

const datecheck = () => {
    const current = moment().format('DD/MM/YYYY');
    const endDate=getDateFormat(endTime)
    if(current == endDate){
      console.log("11111111111---------->")
      return true;
    }else{
      console.log("2222222222---------->")
      return false;
    }
  };

{
        datecheck() === true ?
        <Grid container style={{ 
...
}

1.附加:我建议你在学习React之前先学习JavaScript。我的意思是要真正精通它,否则你会很难学习React。

相关问题