ES6 conditional if statement to check if arrays are empty [duplicate]

女生的网名这么多〃 提交于 2021-02-19 03:13:41

问题


I can't seem to get my jsx es6 react if statement to work.. What am I doing wrong?

const otherVariables = doesntMatter;    

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

How can i check if both arrays are empty?


回答1:


There is a syntax error, you are testing an lambda expression.

You can do something like

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



回答2:


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

And then in the jsx just do:

<div>
  { elementToRender }
</div>


来源:https://stackoverflow.com/questions/37327681/es6-conditional-if-statement-to-check-if-arrays-are-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!