简体   繁体   中英

How to render jsx only when a condition is true using react and typescript?

i want to render a jsx if condition is true using ternary operator and using react and javascript.

what i am trying to do? when the variable admin is true an count is < 0 i donot want to display the button "add". and should display under conditions below

admin && count > 0
!admin 

below is my code,

render = () =>{
    return (
        <button> add </button> //should render this based on condition
    )
}

Could someone help me fix this. thanks.

EDIT: below are the conditions to be displayed

count > 0 && is_admin
count > 0 && !is_admin
count <0 && !is_admin

condition when not to be displayed

count <0 && is_admin

For this case, you can use short circuit evaluation:

render() {
    return (
      <>
        {((admin && count > 0) || !admin) && <button> add </button>}
      </>
    )
}

You can simply add "&&" to render conditionally

render = () => {
    myCondition && (<button>test</button>)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM