Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


How do you set an active state on one component at a time, and remove the active state on all others in React?

I have a basic application, where I click on a dot and it changes the colour to red. I have two dots. However, I want to be able to click on one, give it its active state, then disable the active state the other dot. Currently, I have it working. However, I don't feel confident its the right approach.

In the Dot component, I have logic to set an 'activeDot' class

className={"dot " + (this.props.isActive ? "activeDot" : "")}

I have a function in the main parent component 'handleDot'

handleDot = id => {
    var state = Object.assign({}, this.state);
    for (let i = 0; i < this.state.dots.length; i++) {
      if (i === id) {
        state.dots[i].isActive = true;
      } else {
        state.dots[i].isActive = false;

Here is a link to the project


is there a better way to approach this?

Thanks in advance

1 Answer


  handleDot = id => {
    this.setState(prevState => ({
      dots: prevState.dots.map((dot, i) => ({
        isActive: i === id