写在前面
这个系列的文章没有提供react的系列用法,只提供了一些在实现细节方面的遇到问题的解决方法,因此在学习这个系列内容的时候,已经假定你有了一些react基础。若是没有,推荐学习react初学者教程react
它是惟一有3个操做数的运算符,因此有时又称为三元运算符。通常来讲,三目运算符的结合性是右结合的。
对于条件表达式b ? x : y,先计算条件b,而后进行判断。若是b的值为true,计算x的值,运算结果为x的值;不然,计算y的值,运算结果为y的值。一个条件表达式毫不会既计算x,又计算y。条件运算符是右结合的,也就是说,从右向左分组计算。例如,a ? b : c ? d : e将按a ? b : (c ? d : e)执行。数组
执行过程:app
if(a) { return b; } else { return c; }
在react中,当你在判断以后的某一项不是一个单项构成的时候呢?学习
好比 a ? b : c 中的b由不少<div>构成,怎样才能发挥三目运算符的功能呢?
若是你是这样实现的:spa
{item.question_answer.answer1? <input type="radio" className = "circle" ></input> <span className = "answer"> A. {item.question_answer.answer1}</span> :null}
很明显你的三目运算并不会发挥做用。code
下面教你们两种实现方式:教程
{item.question_answer.answer1? [ (<input type="radio" className = "circle" ></input>), (<span className = "answer" > A. {item.question_answer.answer1}</span>) ] : null}
用 [ ( ) , ( ) , ( ) ] 的数组形式展示b的内容ci
{item.question_answer.answer1? <div className = "mutilrWrapper" key = "index"> <input type="checkbox" className = "circle" i ></input> <span className = "checkboxAnswer answerWrapper" ></span> <img src = {imgURL} className = "selected" /> <span className = "answer" id = "A"> A. {item.question_answer.answer1}</span> </div> : null}
完成!get