I'm trying to add an active class to my buttons in react, however this doesn't add my class and does nothing when I click on a button.
I get no errors in the console and I have similar code for radio buttons which does work but I am not sure how to modify that code to work for my group of buttons.
I can see in the react dev tools that my class nav-active is being applied to the first button as expected but I dont see the black bg color on the button:
<Styled.button isActive="1" className="nav-active onChlick=bound setActiveTab()>
const TestBtn = styled(Button)`
height:40px;
width:auto;
background:#ff00ff;
`
class TopCategories extends React.Component {
constructor() {
super()
this.state = { isActive: 1 }
this.setActiveTab = this.setActiveTab.bind(this)
}
setActiveTab(id) {
this.setState({ isActive: id })
}
render() {
const items = [
{ id: 1, name: 'tab-1', text: 'text', value: '1' },
{ id: 2, name: 'tab-2', text: 'text', value: '2' },
{ id: 3, name: 'tab-3', text: 'text', value: '3' },
{ id: 4, name: 'tab-4', text: 'text', value: '4' },
{ id: 5, name: 'tab-5', text: 'text', value: '5' },
]
const tabs = items.map(item =>
<div key={item.id}>
<TestBtn
isActive={item.id}
className={this.state.isActive === item.id ? 'nav-active' : ''}
onClick={this.setActiveTab}
>
{item.name}
</TestBtn>
</div>,
)
return (
<Container>
<Wrapper>
{tabs}
</Wrapper>
</Container>
)
}
}
export default TopCategories
styles.css
.nav-active { background:#000;}
isActiveget updated when you click and what value it printing. Try for bothitem.idandisactive.isActivethe class you want to add? There's a snippet.isActive { background:#000;}