Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
The stopwatch will have two main states visible to the user. It will be either in a running state, or a stopped state. The buttons on the interface should change based on the running state. We'll start by initializing state in the Stopwatch
component.
Resources
- Conditional (ternary) Operator
- Conditional Rendering – React docs
- Ternary Conditional Operator - React docs
- Ternary Operator – Treehouse workshop
Conditional rendering with if...else:
let button;
if (isRunning) {
button = <button onClick={() => setIsRunning(false)}>Stop</button>;
} else {
button = <button onClick={() => setIsRunning(true)}>Start</button>;
}
return (
<div className="stopwatch">
<h2>Stopwatch</h2>
<span className="stopwatch-time">0</span>
{button}
<button>Reset</button>
</div>
);
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up