1.9 如何根据不同的条件进行不同的内容渲染
一、使用条件语句
iffunction App() { let content const count = 3 if (count > 1) { content = <div> hello </div> } else { content = <p>123</p> } return <>{content}</> } export default App注意:适合做多个条件的渲染
条件运算符(三元运算符):
? :function App() { // let content const count = 3 // if (count > 1) { // content = <div> hello </div> // } else { // content = <p>123</p> // } return <>{count > 3 ? <div> hello </div> : <p>123</p>}</> } export default App注意:适合做多个条件的渲染
逻辑运算符:
&&, ||哪些值不会渲染:布尔值、空字符、null、对象、函数
如何对不渲染的值进行输出:
JSON.stringify(),{undefined + ''}
function App() { return ( <> <div>hello</div> {0 && <div> world </div>} </> ) } export default App注意:适合做一个条件的渲染
switch
最后更新于