Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React Basics (retired) Component Lifecycle Stopwatch State

Better way to handle start/stop

This is just my opinion, but isn’t this the best way to implement the start/stop text?

<button>{this.state.running ? "Stop" : "Start"}</button>

It’s similar to what @jim did, but it doesn’t have the duplicate <button> code.

My question is, did I make a mistake?

2 Answers

You had the right idea!

For your button onClick, use this:

  handleClick () {
    this.setState({ running: !this.state.running });
  }

I know you wrote this 5 months ago, but perhaps other students will see it.

Gabbie Metheny
Gabbie Metheny
33,778 Points

This worked great, thanks Adam!

Yeah. It worked well.

Well never mind, as I kept watching the video, I realized that it would be hard to implement the click handlers this way. :D