How to add a class with React.js?

前端 未结 5 1807
广开言路
广开言路 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:47

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

    Change your render function inside your component to:

    render: function() {
      return 
    },

    And add a function inside :

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

    And inside your component, pass the filter state to the component as a prop:

    return 

    Kids Finder

    {list}

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

提交回复
热议问题