ES6条件if语句检查数组是否为空

IT技术 javascript arrays reactjs
2021-05-22 21:52:50

我似乎无法让我的 jsx es6 react if 语句起作用..我做错了什么?

const otherVariables = doesntMatter;    

return (
...
    <div>
    {if (props.student.length == null && props.teacher.length == null) => (
       <p>empty</p>
    ) : (
       <p>not empty</p>
    )} 
   </div>
...
)

如何检查两个数组是否为空?

2个回答

存在语法错误,您正在测试 lambda 表达式。

你可以做类似的事情

return !!props.student.length && !!props.teacher.length ? <p>not empty</p> : <p>empty</p>;
const elementToRender = props.student.length && props.teacher.length ?  <p>not empty</p> : <p>empty</p>

然后在 jsx 中执行:

<div>
  { elementToRender }
</div>