In my ReactJS component below I'm rendering 3 buttons using React.createElement(). I want to add functionality where the button clicked on gets a 'selected' class and all the other buttons get an 'unselected' class. Furthermore, when the component firsts loads I'd like the first button ('Select All') to have the 'selected' class.
Right now I've written this button logic using jQuery but I'm wondering how I could do this in all React.
class Mychart extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
handleClick(i) {
// for all buttons: remove 'selected' class, add 'unselected' class
$('.custom-btn').removeClass('selected').addClass('unselected');
// for the button we're clicking on: remove the 'unselected' class, add 'selected' class
$('.custom-btn[index='+i+']').removeClass('unselected').addClass('selected');
}
render() {
[{label: 'Select All', label: 'Select Legacy', label: 'Select Kids'}].map((button, i) => {
return React.createElement('button', {className: i === 0 ? 'custom-btn selected' : 'custom-btn unselected', index: i, onClick: () => {this.handleClick(i)} }, button.label)
});
}
}
ReactDOM.render(React.createElement(Mychart), parent);
:focusselector?