Search code examples
reactjstimer

How to use easytimer.js in my React app?


I'm trying to use easytimer.js in my React app (used create-react-app). But I got the following error:

Failed to compile
./src/easytimer.js
  Line 10:  'define' is not defined  no-undef
  Line 11:  'define' is not defined  no-undef

Here's my code.

I tested on codesandbox and it runs without any errors.

Any help would be highly appreciated!


Solution

  • Here is your Solution

    Edit z34pwmy1mm

    Timer.js Code:

    import React, { Component } from "react";
    import EasyTimer from "easytimer";
    
    class App extends Component {
        constructor() {
            super();
    
            this.state = {
                timer: new EasyTimer(),
                timeValues: ""
            };
    
            this.tick = this.tick.bind(this);
        }
    
        componentDidMount() {
            let { timer } = this.state;
            timer.start();
            timer.addEventListener("secondsUpdated", this.tick);
        }
    
        tick(e) {
            let { timer } = this.state;
            const timeValues = timer.getTimeValues().toString();
            this.setState({ timeValues: timeValues });
        }
    
        render() {
            return <div className="App">{this.state.timeValues}</div>;
        }
    }
    
    export default App;