Navigate back to the homepage

freeCodeCamp Pomodoro Clock 01: React Functional Components and Local State Using Hooks

Aryan Jabbari
February 2nd, 2020 · 4 min read

freeCodeCamp Pomodoro Clock 01: React Functional Components and Local State

Welcome back!

This tutorial is the second installment of a tutorial series where I cover the freeCodeCamp Pomodoro Clock project. I’ll be following the spec pretty closely including passing 100% of the tests in the freeCodeCamp test suite.

If you missed the last installment, feel free to read at freeCodeCamp Pomodoro Clock 00: create-react-app Development Environment.

As you read this blog post, don’t forget to stop and try it yourself before I reveal the correct code. You’ll learn a lot more that way!

For those of you who learn better via video, I’ve also created a video walking through these same steps:


By the end of this tutorial, you should:

  • understand how to create a new functional component
  • understand how to read and set state in a functional component
  • understand how to bind a function to a button’s click event handler
  • how to convert seconds into minutes using Moment.js

To achieve these goals, we’ll create three components:

  • a Break component that tracks the break time
  • a Session component that tracks the session time, and
  • a TimeLeft component that will display the time left in the current session
    • this component will share the data set by the Session component (and, in a later tutorial, the Break component)

Now, start your development server using npm start and let’s get started!

Break Component

Create a New Functional Component

Inside of your /src directory, create a /components directory. We’ll use this directory to keep our file structure nice and tidy.

Now, inside your /components directory, create a new file: Break.jsx. Initialize the file with functional component boilerplate:

1// /src/components/Break.jsx
2import React from 'react';
4const Break = () => {
5 return <div></div>;
8export default Break;

Move the <p id=“break-label”>Break</p> line in src/App.js inside the /src/components/Break.jsx <div> element. Finally, import the Break component into your App.js file and render it in between the <div className=“App”> element:

1// /src/App.js
2import React from 'react';
3import './App.css';
4import Break from './components/Break'; // 👈 import Break here
6function App() {
7 return (
8 <div className="App">
9 <Break />
10 </div>
11 );
14export default App;

If you did everything correctly and visit http://localhost:3000/, nothing should have changed since last time. The text ”Break” should be rendered in the center of your browser.

Initialize Break Length using React State (and useState)

Since we’re starting with break, let’s tackle a freeCodeCamp User Story. Specifically, we’ll tackle: *User Story #5: I can see an element with a corresponding id=“break-length”, which by default (on load) displays a value of 5.”.

As per the spec, we’ll render the number of minutes to the user. However, since we’ll need to use seconds when we implement the countdown feature, we’ll store the data as seconds. To store data that can be modified by the user and forces the component to re-render on change (basically, the new state will be rendered In the browser), we’ll use React state. More specifically, we’ll use the React state hook in our Break component.

The syntax for useState() is as follows (we’ll use favoriteColor as an example):

1const [favoriteColor, setfavoriteColor] = useState('red');

Here, favoriteColor is the actual variable that is initialized to 'red'. We can change the value of favoriteColor by calling setFavoriteColor with a new string: setFavoriteColor(‘blue’).

Let’s add state to the Break component! On the first line inside /src/components/Break.jsx, write: const [breakLengthInSeconds, setBreakLengthInSeconds] = useState(300); (where 300 is 5 minutes in seconds).

Then, render breakLengthInSeconds below the existing <p> tag inside a <p> tag of its own (don’t forget id=“break-length”.to prepare to pass another freeCodeCamp test)!

If you did everything correctly, /src/components/Break.jsx should look like:

1// /src/components/Break.jsx
2import React, { useState } from 'react';
4const Break = () => {
5 const [breakLengthInSeconds, setBreakLengthInSeconds] = useState(300);
6 return (
7 <div>
8 <p id="break-label">Break</p>
9 <p id="break-length">{breakLengthInSeconds}</p>
10 </div>
11 );
14export default Break;

You’ll notice the browser renders out ”300” instead of the requested ”5”. No worries, we’ll fix that later.

Add Plus & Minus Buttons with Click Event Handlers

Let’s start by writing the functions that’ll be called by the plus and minus buttons, respectively. The plus button should add one minute (60 seconds) to the break length while the minus button does the opposite (without allowing the number of seconds to drop below 0). In Break.jsx (between declaring setBreakLengthInSeconds and returning the JSX), write the following two functions:

1const decrementBreakLengthByOneMinute = () => {
2 const newBreakLengthInSeconds = breakLengthInSeconds - 60;
3 if (newBreakLengthInSeconds < 0) {
4 setBreakLengthInSeconds(0);
5 } else {
6 setBreakLengthInSeconds(newBreakLengthInSeconds);
7 }
9const incrementBreakLengthByOneMinute = () => setBreakLengthInSeconds(breakLengthInSeconds + 60);

To handle events in React, we need to remember to use camel case for event listener attributes in our HTML elements. For example,

1<button onClick={activateLasers}>Activate Lasers</button>

Notice the capital ”C” here.

In the JSX part of Break.jsx, add plus and minus buttons (with the ids as requested in freeCodeCamp) that call the two functions we wrote above . If you did everything correctly, your Break.jsx should look like this:

1// src/components/Break.jsx
2import React, { useState } from 'react';
4const Break = () => {
5 const [breakLengthInSeconds, setBreakLengthInSeconds] = useState(300);
7 const decrementBreakLengthByOneMinute = () => {
8 const newBreakLengthInSeconds = breakLengthInSeconds - 60;
9 if (newBreakLengthInSeconds < 0) {
10 setBreakLengthInSeconds(0);
11 } else {
12 setBreakLengthInSeconds(newBreakLengthInSeconds);
13 }
14 };
15 const incrementBreakLengthByOneMinute = () => setBreakLengthInSeconds(breakLengthInSeconds + 60);
16 return (
17 <div>
18 <p id="break-label">Break</p>
19 <p id="break-length">{breakLengthInSeconds}</p>
20 <button id="break-increment" onClick={incrementBreakLengthByOneMinute}>
21 +
22 </button>
23 <button id="break-decrement" onClick={decrementBreakLengthByOneMinute}>
24 -
25 </button>
26 </div>
27 );
30export default Break;

Now go back to the running app in your browser. The buttons should add and subtract 60 seconds to your break time.

Converting Seconds to Minutes using Moment.js

Let’s get rid of the ”300” that is rendered and, instead, render the ”5” the was requested of us by the freeCodeCamp spec.

Dealing with time is famously difficult. Sure, converting from seconds to minutes is easy enough (just divide by 60, right) but why write the code? Moment.js is an spectacular library that makes dealing with time easy (and we’ll use it later in this project when displaying the time left).

Let’s start by installing moment to our project:

1npm install moment

We’ll use Moment durations to convert from seconds to minutes. To create a duration, the syntax is moment.duration(timeCount, unitOfTime). For example, since our units are in seconds, we’ll create a direction with moment.duration(breakLengthInSeconds, ’s’) . To convert that into minutes, just chain a call to .minutes() at the end. Save this to a variable and render out that variable.

1// /src/components/Break.jsx
3import moment from 'moment';
4import React, { useState } from 'react';
6const Break = () => {
7 const [breakLengthInSeconds, setBreakLengthInSeconds] = useState(300);
9 const decrementBreakLengthByOneMinute = () => {
10 const newBreakLengthInSeconds = breakLengthInSeconds - 60;
11 if (newBreakLengthInSeconds < 0) {
12 setBreakLengthInSeconds(0);
13 } else {
14 setBreakLengthInSeconds(newBreakLengthInSeconds);
15 }
16 };
17 const incrementBreakLengthByOneMinute = () => setBreakLengthInSeconds(breakLengthInSeconds + 60);
19 const breakLengthInMinutes = moment.duration(breakLengthInSeconds, 's').minutes(); // the seconds to minutes conversion is HERE!
20 return (
21 <div>
22 <p id="break-label">Break</p>
23 {/* Note the variable change below */}
24 <p id="break-length">{breakLengthInMinutes}</p>
25 <button id="break-increment" onClick={incrementBreakLengthByOneMinute}>
26 +
27 </button>
28 <button id="break-decrement" onClick={decrementBreakLengthByOneMinute}>
29 -
30 </button>
31 </div>
32 );
35export default Break;

You should now be passing “User Story 5” in your freeCodeCamp test suite.

Session Component

The session component will be in a new file (/src/components/Session) is almost identical to the break component with changes to variable and HTML id names (to match those in the freeCodeCamp test suite). Additionally, as per the freeCodeCamp test suite, the value of the initial session length should be equal to 25 minutes.


1import React from 'react';
2import './App.css';
3import Break from './components/Break';
4import Session from './components/Session';
6function App() {
7 return (
8 <div className="App">
9 <Break />
10 <Session />
11 </div>
12 );
15export default App;


1import moment from 'moment';
2import React, { useState } from 'react';
4const Session = () => {
5 const [sessionLengthInSeconds, setSessionLengthInSeconds] = useState(60 * 25);
7 const decrementSessionLengthByOneMinute = () => {
8 const newSessionLengthInSeconds = sessionLengthInSeconds - 60;
9 if (newSessionLengthInSeconds < 0) {
10 setSessionLengthInSeconds(0);
11 } else {
12 setSessionLengthInSeconds(newSessionLengthInSeconds);
13 }
14 };
15 const incrementSessionLengthByOneMinute = () =>
16 setSessionLengthInSeconds(sessionLengthInSeconds + 60);
18 const sessionLengthInMinutes = moment.duration(sessionLengthInSeconds, 's').minutes();
19 return (
20 <div>
21 <p id="session-label">Session</p>
22 <p id="session-length">{sessionLengthInMinutes}</p>
23 <button id="session-increment" onClick={incrementSessionLengthByOneMinute}>
24 +
25 </button>
26 <button id="session-decrement" onClick={decrementSessionLengthByOneMinute}>
27 -
28 </button>
29 </div>
30 );
33export default Session;

Open up your freeCodeCamp test suite and run the tests. You should now be passing seven tests!

You Made It! 👩‍💻 👏

Way to go! You created the first two components needed for the freeCodeCamp Pomodoro Clock.

If you enjoyed this tutorial, follow me at:

If at any point you got stuck in this tutorial, please review the code on GitHub.

If you are interested in the freeCodeCamp Random Quote Machine implementation, please take a look at my videos on YouTube.

📧 Join my email list and get notified about new content

Stay updated with all my blog posts and future randomness!

More articles from The WebDev Coach

freeCodeCamp Pomodoro Clock 00: create-react-app Development Environment

freeCodeCamp Pomodoro Clock 00: create-react-app Development Environment Congratulations on getting this far in your coding journey! You…

January 19th, 2020 · 3 min read

User Registration with a NestJS GraphQL Server and Prisma

User Registration with a NestJS GraphQL Server and Prisma Phew, it’s been a while. Let’s get back to creating our NestJS server leveraging…

January 13th, 2020 · 3 min read
© 2018–2020 The WebDev Coach
Link to $ to $ to $ to $ to $ to $