简体   繁体   中英

toggle react component using hide show classname

I'm using this npm https://www.npmjs.com/package/classnames

<ul className={classnames('hide', {'show': props.openFilter, 'hide': !props.openFilter} )}>
</ul>

I do this.setState({openFilter: !this.state.openFilter}) , but the toggle won't work, it show the first time, what's the problem?

您可能正在寻找...

<ul className={ this.state.openFilter ? "show" : "hide" }></ul>

Without seeing the rest of the function I can only presume you intended to do the following instead.

<ul
  className={classnames({
    show: this.state.openFilter,
    hide: !this.state.openFilter
  })}
/>

For clarity, classnames accepts arguments of strings and/or objects. String arguments appear as written is their value is truthy. Whereas object arguments key are transformed into classes where their values are truthy.

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