How to add a class with React.js?

前端 未结 5 1802
广开言路
广开言路 2020-12-23 13:32

I need add class \"active\" after click to button and remove all other \"active\" classes.

Look here please: https://codepen.io/azat-io/pen/RWjyZX

va         


        
相关标签:
5条回答
  • 2020-12-23 13:40

    you can also use pure js to accomplish this like the old ways with jquery

    try this if you want a simple way

     document.getElementById("myID").classList.add("show-example");
    
    0 讨论(0)
  • 2020-12-23 13:41

    this is pretty useful:

    https://github.com/JedWatson/classnames

    You can do stuff like

    classNames('foo', 'bar'); // => 'foo bar'
    classNames('foo', { bar: true }); // => 'foo bar'
    classNames({ 'foo-bar': true }); // => 'foo-bar'
    classNames({ 'foo-bar': false }); // => ''
    classNames({ foo: true }, { bar: true }); // => 'foo bar'
    classNames({ foo: true, bar: true }); // => 'foo bar'
    
    // lots of arguments of various types
    classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
    
    // other falsy values are just ignored
    classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
    

    or use it like this

    var btnClass = classNames('btn', this.props.className, {
      'btn-pressed': this.state.isPressed,
      'btn-over': !this.state.isPressed && this.state.isHovered
    });
    
    0 讨论(0)
  • 2020-12-23 13:43

    Taken from their site.

    render() {
      let className = 'menu';
      if (this.props.isActive) {
        className += ' menu-active';
      }
      return <span className={className}>Menu</span>
    }
    

    https://reactjs.org/docs/faq-styling.html

    0 讨论(0)
  • 2020-12-23 13:47

    Since you already have <Tags> component calling a function on its parent, you do not need additional state: simply pass the filter to the <Tags> component as a prop, and use this in rendering your buttons. Like so:

    Change your render function inside your <Tags> component to:

    render: function() {
      return <div className = "tags">
        <button className = {this._checkActiveBtn('')} onClick = {this.setFilter.bind(this, '')}>All</button>
        <button className = {this._checkActiveBtn('male')} onClick = {this.setFilter.bind(this, 'male')}>male</button>
        <button className = {this._checkActiveBtn('female')} onClick = {this.setFilter.bind(this, 'female')}>female</button>
        <button className = {this._checkActiveBtn('blonde')} onClick = {this.setFilter.bind(this, 'blonde')}>blonde</button>
      </div>
    },
    

    And add a function inside <Tags>:

    _checkActiveBtn: function(filterName) {
      return (filterName == this.props.activeFilter) ? "btn active" : "btn";
    }
    

    And inside your <List> component, pass the filter state to the <tags> component as a prop:

    return <div>
      <h2>Kids Finder</h2> 
      <Tags filter = {this.state.filter} onChangeFilter = {this.changeFilter} />
      {list}
    </div>
    

    Then it should work as intended. Codepen here (hope the link works)

    0 讨论(0)
  • 2020-12-23 13:50

    It is simple. take a look at this

    https://codepen.io/anon/pen/mepogj?editors=001

    basically you want to deal with states of your component so you check the currently active one. you will need to include

    getInitialState: function(){}
    //and 
    isActive: function(){}
    

    check out the code on the link

    0 讨论(0)
提交回复
热议问题