3

I want to set the active class dynamically in react.js but it's not working! I'm using the setState() method to change the selected item.

this line of code not work . className={selectedCategoryId === item.id ? 'active' : ''}

I think the setState() function does not work correctly...

const {open, selectedProduct, productCategory, filteredProducts, selectedCategoryId} = this.state;
const categoryItems = productCategory.map((item) =>
    <a key={item.id}
       onClick={() => this.handleFilter(item.id)}
       className={selectedCategoryId === item.id ? 'active' : ''}
       // className={()=>this.isActive(item.id)}
       className="pointer"
    >{item.value}</a>
);

this does not change the class:

handleFilter = (id) => {
    const filteredItem = this.state.productList.filter(x => x.categoryId == id);
    this.setState({filteredProducts: filteredItem, selectedCategoryId: id});  
}

but this change the className correctly when select I all tags:

handleRemoveFilter = () => {
    this.setState({filteredProducts: this.state.productList, selectedCategoryId: 0});
}
//-------------------------------
<div className="tag-list">
    <a  onClick={this.handleRemoveFilter}
        className="pointer"
        className={ this.state.selectedCategoryId === 0 ? 'active' : ''}
    >All tags</a>
    {categoryItems}
</div>
5
  • Hi, I want to help but I didn't really understand the problem. Please try to be clear about what you EXPECT to happen and what IS happening. Commented Mar 26, 2019 at 8:03
  • please put here relevant code frok and show us link(it will be easier to help you):stackblitz.com/edit/react-pzyyha Commented Mar 26, 2019 at 8:06
  • 1
    Why did you use "className" twice ? Commented Mar 26, 2019 at 8:07
  • Can you share the whole component? Commented Mar 26, 2019 at 8:31
  • I Really didn't thinks that made the problem! thanks Commented Mar 26, 2019 at 9:10

4 Answers 4

2

If setState() works well, try this :

<a onClick={this.handleRemoveFilter}
        className={ this.state.selectedCategoryId === 0 ? 'pointer active' : 'pointer'}
>All tags</a>

Sign up to request clarification or add additional context in comments.

Comments

1

One of the most common ways is to use classnames which you can conditionally joining classNames together

var classNames = require('classnames');

class Button extends React.Component {
  // ...
  render () {
    var btnClass = classNames({
      btn: true,
      'btn-pressed': this.state.isPressed,
      'btn-over': !this.state.isPressed && this.state.isHovered
    });
    return <button className={btnClass}>{this.props.label}</button>;
  }
}

2 Comments

i wanna set className dynamically but className={selectedCategoryId === item.id ? 'active' : ''} not work .
The correct way to write is className={classNames({active: selectedCategoryId === item.id})}
1

We can toggle class name dynamically like below,

const [islight, setIslight] = useState(false)
const toggle = () => {
    setIslight(!islight)
}

return (
   <div className={`block ${islight ? "blocklight" : "blockdark"}`}>
      <h2>Hello World</h2>
   </div>
)

Comments

0

store classname in state along with selected item. You can just update the classname in state whenever required.

for eg,

  <a key={item.id}
            onClick={() => this.handleFilter(item.id)}
            className={this.state.activeClassName}

where active classname can be updated inside handlefilter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.