Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add & Remove CSS classes with React

I am new to React. I have a few buttons in a button group:

<div className="btn-group">
    <button className="btn btn-mini btn-default" onClick={() => this.changeDataType("type1")} >Button1</button>
    <button className="btn btn-mini btn-default" onClick={() => this.changeDataType("type2")} >Button2</button>
    <button className="btn btn-mini btn-default" onClick={() => this.changeDataType("type3")} >Button3</button>
</div>

Whenever the user clicks on one of the buttons, this button should become the active, selected one. I found out that I need to add the CSS class active to the corresponding button, but I am not sure how to implement this.

I thought about this for a bit. I have a changeDataType function connected to my buttons, in which I do some other stuff. Would I then, in there, somehow manipulate the CSS?

So I guess my questions are first, how to target the button I need to target, and second, how I could change that button's CSS with React.

like image 560
George Welder Avatar asked May 04 '17 15:05

George Welder


People also ask

What is “add”?

The term ADD is commonly used to describe what clinicians now diagnose as Predominantly Inattentive Type ADHD. This quieter presentation of attention deficit disorder — not associated with hyperactivity — is thought to be more common among girls and women. Common symptoms of “ADD” include:

What is the verb for add?

transitive verb 1 : to join or unite so as to bring about an increase or improvement adds 60 acres to his land wine adds a creative touch to cooking 2 : to say further : append Do you have anything else to add to the discussion? 3 : to combine (numbers) into an equivalent simple quantity or number

What is the difference between add and ADHD type a?

A: ADHD, Primarily Inattentive Type. People who describe themselves as having ADD most likely have inattentive type ADHD. Symptoms include forgetfulness and poor focus, organization, and listening skills. Inattentive ADHD often resembles a mood disorder in adults, while it’s seen as spacey, apathetic behavior in children, particularly girls.

What is combined ADHD?

Combined ADHD is found in people who have both types of ADHD. Keep in mind that ADHD may manifest differently, especially in children. Girls and boys typically have different types of symptoms, and adult diagnoses may also be based on different criteria. An ADHD diagnoses for you or your child doesn’t mean that anything is wrong with them.


1 Answers

In react when state changes react re-renders. In your case if you want to change the way something looks then you probably want to force another render. What you can do is have the className be part of state and then update the state when you want, causing the component to re-render with the new className. For example:

constructor() {
    super();
    this.state = {
        className: 'btn'
    }
}

render () {
    return (
        <Button className={this.state.className}>Click me</Button>
    )
}

Since the className is bound to state updating state will cause the button to render again with the new className. This can be done like this:

updateClass() {
    let className = this.state.className;
    className = className + ' ' + 'btn-primary'
    this.setState({className})
}

This is an example of the function you can call on the click of a button and update the className for the button.

like image 183
Chaim Friedman Avatar answered Sep 27 '22 01:09

Chaim Friedman