Javascript 反应中的多个If状态

Javascript 反应中的多个If状态,javascript,reactjs,Javascript,Reactjs,我试图在满足条件的基础上输出一个结果,但只输出一个结果,我不知道为什么我总是得到相同的结果。这是我的密码 const newDate = new Date() const startDate= new Date("Feb 1, 2021"); const endDate= new Date("Dec 31, 2021"); const newDateOutPut =()=>{ if(newDate < startDate){

我试图在满足条件的基础上输出一个结果,但只输出一个结果,我不知道为什么我总是得到相同的结果。这是我的密码

const newDate = new Date()
const startDate= new Date("Feb 1, 2021");
const endDate= new Date("Dec 31, 2021");


const newDateOutPut =()=>{
   if(newDate < startDate){
     return <small  className="badge badge-warning">Not Active</small>
   }else if (newDate >= startDate && newDate <= endDate  ){
     return <small  className="badge badge-success"> Active</small>
   }else{
    return <small  className="badge badge-secondary"> Completed</small>
   }
 }


return(
<>
{newDateOutPut()}
</>
const newDate=新日期()
const startDate=新日期(“2021年2月1日”);
const endDate=新日期(“2021年12月31日”);
常量newDateOutPut=()=>{
如果(新日期<开始日期){
返回未激活

}else if(newDate>=startDate&&newDate当然,在这种情况下@gabriel只有一个输出,这就是if/else if/else条件的工作方式,返回第一个truthy

如果您希望针对仅为
true
的条件显示多个结果,我建议您将代码重构为如下内容:

const newDate = new Date()
const startDate= new Date("Feb 1, 2020");
const endDate= new Date("Dec 31, 2020");

const newDateOutPut = () => {
    return (
        <>
            {
                newDate < startDate ? <small  className="badge badge-warning">Not 
                Active</small> : <></>
            }
            {
                newDate >= startDate && newDate <= endDate ? <small  
                className="badge badge-success">Active</small> : <></>
            }
            {
                !(newDate < startDate) && !(newDate >= startDate && newDate <= 
                endDate) ? <small  className="badge badge-secondary"> 
                Completed</small> : <></>
            }
        </>
    )
}

const newDate=新日期()
const startDate=新日期(“2020年2月1日”);
const endDate=新日期(“2020年12月31日”);
常量newDateOutPut=()=>{
返回(
{
newDatenewDate>=startDate&&newDate=startDate&&newDate
newDate
不小于
startDate
newDate
构建到当前日期。我们现在是2021年,而
startDate
是2020年。

今天的日期大于2020年2月1日。您也不检查结束日期,所以一切都将在e中结束lse if当它大于开始时。是否尝试
console.log()
newDate和startDate的值?不能让
const newDate
同时存储日期和作为函数。旁注:两个日期不是
=
,没有第三种情况。你真的能够运行这个吗?因为有两个
const
具有相同的名称(
newDate
)函数名是一个输入错误,请忽略它。你可以使用任何名称。我更新了问题,它是一个输入错误。它在stackblitz中工作,检查它。我仍然有相同的输出“活动”