我正在编写奇特的登录模式,并遇到了一些令人困惑的问题。我需要在 React 中切换模式。并认为这是最优雅的方式。但这安全吗?或者好的做法。
className={ 'modal ' + ((this.state.showLoginModal && 'is-active') || '') }
我希望这在某些浏览器上不会失败。
最佳答案
使用该语法完全没问题,但我更喜欢使用 ES6 中的刻度和三元运算符:
className={`modal ${this.state.showLoginModal ? 'is-active' : ''}`}
关于javascript - '+ ((visible && "is-active") || "")' 是一个好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018801/