Home > Web Front-end > JS Tutorial > body text

A description of ternary operations in react.js

韦小宝
Release: 2018-03-14 10:01:24
Original
2503 people have browsed it

This article is about the ternary operation in react.js. If you are not familiar with or don’t understand the ternary operation in react.js, we can take a look at this article together! Without so much nonsense, let’s get straight to the topic!

1. Ternary operation

//在js中定义一个style属性,可参见reactjs(一)
var style = {
   background-color:"green";
}
Copy after login
var Message = React.createClass({
    render:function(){
       return{               //三元运算要被包裹在花括号中。因为花括号不能写在return的第一层,所以必须要在外围嘛加一个p标签
           <p>
             {false?<h1 style={style}>齐天大圣</h1>}           </p>
       }
    }

})
Copy after login

Application of if conditional statement

var Message = React.createClass({
    render:function(){
          if(true){
               return(
                <h1>孙悟空</h1>
            )    
            };
})
Copy after login

Comparison operation Fu

var Message = React.createClass({
    render:function(){
         return(
         <p>
            {
                true  || <h1 style = {style}>孙悟空</h1>
}
         </p>
)
            };
})
Copy after login

The above is all the content of this article. I hope everyone who doesn’t know much about the ternary operation in react.js will take a good look!

Related recommendations:

React.js props

The above is the detailed content of A description of ternary operations in react.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template