Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how correctly use ternary operator in react js?

I'm using React. I've just started recently and have some kind of knowledge gap in syntax of javascript. I don't know how to write correctly code in order to toggle element condition, from display: block to display: none. I'm trying to use ternary operator, but I don't know how to do it.

 toggleFilters = (e) => {
    this.state.showOn === true ? {e.style.display ? 'block' : 'none'}

    this.setState({
        showOn: !this.state.showOn,
    })
}
like image 508
foxer bixer Avatar asked Sep 12 '25 21:09

foxer bixer


1 Answers

Easier way to do it, is this. This says that your e.style.display is block if your shownOn is true else your e.style.display is false.

e.style.display = this.state.showOn ? 'block' : 'none'}
like image 183
learner Avatar answered Sep 14 '25 11:09

learner