Ads Top

How to use if else condition in react js?


Condition in the React:
If else in react basically this is a ternary operator if you know about ternary operator then it’s so easy.

Ternary Operator syntax:
condition ? true : false

Here is described as three type also, you can use more type…

Note: but need to use return in HTML template then use as below:

export default (props=> {

 const renderanyNumb = () => props.anyNum !='' ? <div> Any Text {props.anyNum}</div> : null;

return(
        <div>
            Phone Number: <span>{props.anyNum }</span>  { renderanyNumb()}
       </div>
    )
}

We need to use directly as below code:
  <div>
{props.anyNum !='' && <div> Any Text {props.anyNum}</div>}
 </div>

{props.anyNum !='' ? <div> Any Text {props.anyNum}</div> : null}


No comments:

Copyright Reserved to Anything Learn. Powered by Blogger.