# Please Review my React Pomodoro Clock

**URL:** <https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281>\
**Category:** I Built This\
**Created:** [June 21, 2018, 11:46am UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281 "2018-06-21T11:46:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellereeeee](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ellereeeee/32/29202_2.png) [@ellereeeee](https://forum.freecodecamp.org/u/ellereeeee)\
**Post date:** [June 21, 2018, 11:46am UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/1 "2018-06-21T11:46:46Z")

</div>

Please review my [Pomodoro Clock](https://ellereeeee-pomodoro-clock.netlify.com/)! It’s my first app built in React.

You can find my source code [here](https://github.com/ellereeeee/pomodoro-clock).

I had some issues with SVG filters making my app slow in Firefox mobile that I [couldn’t resolve](https://stackoverflow.com/questions/50885061/drop-shadow-css-function-makes-app-laggy-in-firefox-mobile).

---

<div class="post-metadata">

**Author:** ![Diego\_Perez](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Diego\_Perez](https://forum.freecodecamp.org/u/Diego_Perez)\
**Post date:** [June 21, 2018, 11:14pm UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/2 "2018-06-21T23:14:10Z")

</div>

Hi @ellereeeee 🙂 ,

##### README (GitHub)

- Is a really good readme, but I couldn’t find a link to the pomodoro:  
(There is one in the description(?) of the project in Github, but not in the readme)

##### pomodoro-clock/src/components/PomodoroTimer.jsx

- Is there any reason not to use the second form of setState() ?:

```auto
handleIncrementTime = () => {
    this.setState({ state: (this.state.time += 300000) });
  };
  handleDecrementTime = () => {
    if (this.state.time > 300000) {
      this.setState({ time: (this.state.time -= 300000) });
    }
  };

```

Reacts Documentation:

[https://reactjs.org/docs/state-and-lifecycle.html](https://reactjs.org/docs/state-and-lifecycle.html)

```auto
this.setState((prevState, props) => ({
  counter: prevState.counter + props.increment
}));

```

* * *

Cheers and happy coding 🙂

---

<div class="post-metadata">

**Author:** ![ellereeeee](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ellereeeee/32/29202_2.png) [@ellereeeee](https://forum.freecodecamp.org/u/ellereeeee)\
**Post date:** [June 22, 2018, 12:03am UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/3 "2018-06-22T00:03:56Z")

</div>

Wow thank you Diego 🙂!

---

<div class="post-metadata">

**Author:** ![ellereeeee](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ellereeeee/32/29202_2.png) [@ellereeeee](https://forum.freecodecamp.org/u/ellereeeee)\
**Post date:** [June 22, 2018, 12:02pm UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/4 "2018-06-22T12:02:22Z")

</div>

Hey @Diego_Perez,

I looked at the documentation for the second form of `setState` and it says you’re supposed to use it when you’re updating the state with props, right? It looks like it solves an asynchronicity issue between the two.

In my case I’m not updating state with props; I’m just adding or subtracting 5 minutes to or from the state. Is there a way I could use the second form of `setState`?

---

<div class="post-metadata">

**Author:** ![Diego\_Perez](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Diego\_Perez](https://forum.freecodecamp.org/u/Diego_Perez)\
**Post date:** [June 22, 2018, 9:42pm UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/5 "2018-06-22T21:42:52Z")

</div>

Hi @ellereeeee,

I cloned the repo and I am getting warnings:

```sh
Compiled with warnings.

./src/components/Info.jsx
  Line 16: Using target="_blank" without rel="noopener noreferrer" is a security risk: see https://mathiasbynens.github.io/rel-noopener react/jsx-no-target-blank

./src/components/PomodoroTimer.jsx
  Line 17: Do not mutate state directly. Use setState() react/no-direct-mutation-state
  Line 21: Do not mutate state directly. Use setState() react/no-direct-mutation-state
  Line 33: Expected '===' and instead saw '==' eqeqeq
  Line 52: Expected '===' and instead saw '==' eqeqeq
  Line 78: Expected '===' and instead saw '==' eqeqeq
  Line 109: Expected '===' and instead saw '==' eqeqeq

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.   

```

The important part is:

```sh
  Line 17: Do not mutate state directly. Use setState() react/no-direct-mutation-state

```

### Changes

1. I added a constructor to PomodoroTimer.jsx

2. Now `state` is `this.state` and is inside the constructor

```js
class PomodoroTimer extends Component {
 constructor(props) {
  super(props); 
  this.state = {
    toggleInfo: false,
    timerActive: false,
    time: 1500000,
    timerType: "Pomodoro",
    offsetModifier: 1
  }; 
 } 

```

1. I modified the handleIncrementTime function:

```js
handleIncrementTime = () => this.setState((prevState) => ({ time: prevState.time += 300000 }));

```

#### Result

- There is no warning for handleIncrementTime function

```sh
Compiled with warnings.

./src/components/Info.jsx
  Line 16: Using target="_blank" without rel="noopener noreferrer" is a security risk: see https://mathiasbynens.github.io/rel-noopener react/jsx-no-target-blank

./src/components/PomodoroTimer.jsx
  Line 25: Do not mutate state directly. Use setState() react/no-direct-mutation-state
  Line 37: Expected '===' and instead saw '==' eqeqeq
  Line 56: Expected '===' and instead saw '==' eqeqeq
  Line 82: Expected '===' and instead saw '==' eqeqeq
  Line 113: Expected '===' and instead saw '==' eqeqeq

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

```

#### suggestions

1. Refactor handleDecrementTime (using functional setState [second form])

2. Refactor handleIncrementTime, handleDecrementTime, handleStateTime:  
Following the pattern: “event handler method on the class”

Reactjs documentation:

> When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class. For example, this Toggle component renders a button that lets the user toggle between “ON” and “OFF” states:

```js
class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState(prevState => ({
      isToggleOn: !prevState.isToggleOn
    }));
  }

```

Cheers and happy coding 🙂

note:  
I suspect that the problem with Firefox mobile can be because the `state` is not correctly updated.

---

<div class="post-metadata">

**Author:** ![ellereeeee](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ellereeeee/32/29202_2.png) [@ellereeeee](https://forum.freecodecamp.org/u/ellereeeee)\
**Post date:** [June 23, 2018, 4:50am UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/6 "2018-06-23T04:50:23Z")

</div>

Thanks again Diego! I get it now; it’s all about not modifying state directly.

I solved the issue using the second form of setState like so:

```auto
  handleIncrementTime = () =>
    this.setState(prevState => ({ time: (prevState.time += 300000) }));
  handleDecrementTime = () => {
    if (this.state.time > 300000) {
      this.setState(prevState => ({ time: (prevState.time -= 300000) }));
    }
  };

```

I have a couple questions regarding your other suggestions though. Isn’t making a constructor and putting state inside a preference in this situation? I’m using [public class fields](https://tc39.github.io/proposal-class-public-fields/) to declare state outside the constructor and inside the class body and everything seems to work fine.

Also, aren’t I already using the “event handler method on the class” for my handlers? I understand that as meaning just declaring the handlers in the class body.

Unfortunately the laggy issue in Firefox mobile remains, but I’m pretty sure it has to do with poor SVG performance in that browser. Removing the SVG filter property from the radial timer makes my app as snappy as in other browsers.

---

<div class="post-metadata">

**Author:** ![Diego\_Perez](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Diego\_Perez](https://forum.freecodecamp.org/u/Diego_Perez)\
**Post date:** [June 23, 2018, 2:52pm UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/7 "2018-06-23T14:52:42Z")

</div>

Hi @ellereeeee,

> [@ellereeeee](#):
>
> I have a couple questions regarding your other suggestions though. Isn’t making a constructor and putting state inside a preference in this situation? I’m using public class fields to declare state outside the constructor and inside the class body and everything seems to work fine.
> 
> Also, aren’t I already using the “event handler method on the class” for my handlers? I understand that as meaning just declaring the handlers in the class body.

I didn’t know about the `public class fields`, thanks for explain it 🙂 .

> [@ellereeeee](#):
>
> Unfortunately the laggy issue in Firefox mobile remains, but I’m pretty sure it has to do with poor SVG performance in that browser. Removing the SVG filter property from the radial timer makes my app as snappy as in other browsers.

I see, nothing to do then.

Cheers and happy coding 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:48pm UTC](https://forum.freecodecamp.org/t/please-review-my-react-pomodoro-clock/202281/8 "2021-06-01T13:48:50Z")

</div>

Closed on June 1, 2021, 1:48pm UTC


