Recreate a stopwatch in React. Your final product should function similarly to this deployed version of the component.
- Fork and clone this repo
- Create a react app in this folder by running
npx create-react-app . - Start the development server locally with
$ npm start, then visitlocalhost:3000in the browser to view app
We're going to put everything into a component called Stopwatch. To create all the pieces found in the deployed version, we'll need:
- an
h1tag that shows the stopwatch time - a
divto contain our three buttons - three buttons for
reset,start, andpause
Once you have that, then you'll need to add functionality to our app!
-
Initialize state with a
counterproperty to track the incrementing time. (You may need additional state properties later on) -
Create a
handleStartmethod that calls setInterval() and updatescounterevery second. (Make sure to usesetStateto update) -
Include an
onClickattribute on the start button which callshandleStart -
Replace the
0in theh1heading with yourcounter. -
Next you will need to create
onClickevent attributes for reset and pause and associated methods for each. -
Finally, you'll want to limit which buttons can be pressed when.
- Start button can't be pushed if there's a count already going
- Pause and reset buttons can only be pushed if there's an active count
Hints...Shhhhhh!
- It might be useful to rethink your state and what properties it should have to accomplish all the tasks!
- You might also want to include an additional attribute in your constructor method (but outside of state) that keeps track of your current interval. (Think about the pros and cons of putting this variable in state vs outside of it)
- You can use
clearIntervalto stop the current interval! - Use the
!operator to toggle boolean values! - When limiting button presses, think about how each value in state tells you what is going on! For example, if there is an interval set, that means there is count going!
- Add some styling! You can make your stopwatch look like the demo, or do something else!
- When the stopwatch is at 0, the display time should be a different style.
- When the user first visits the page, the only button that should be active is "Start".
- The other buttons should be unlocked only after the user clicks "start"
- Have multiple Stopwatch components rendered on the page so the User can set off multiple timers.