Giter Site home page Giter Site logo

Comments (22)

thibaultboursier avatar thibaultboursier commented on May 30, 2024 1

OK! Thanks for opening this issue. I'll let you have a look on PR.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024 1

I'm writing "use-timer": "thibaultboursier/use-timer#fix-start-function-reference-issue" in dependencies list. It is downloading without any problem, but project is not starting after that.

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

Hello @dancheskus,

Thanks for opening issue.
It's better if you provide me a reproductible issue, on StackBlitz for example.

I created an example: https://stackblitz.com/edit/use-timer-issue-47?file=index.tsx
Inside, we have a useEffect, for the autostart purpose, which has start function inside its dependencies array.
When we click on increment button, component is re-rendered, but timer does not start a new time.

I need more informations.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

Timer should be decremental to see this bug. There also should be endTime.

https://stackblitz.com/edit/use-timer-issue-47-pfnhg7?file=index.tsx

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

I see. There should be some issue with start function reference, which causes a re-render as you declared it inside dependencies array. I'm going to investigate. Anyway, if you want to be sure that start function is executed only once, you should remove start function from dependencies array; it works.

const { time, start, pause, reset, isRunning } = useTimer({
    initialTime: 3,
    endTime: 0,
    timerType: "DECREMENTAL"
  });

  useEffect(()=>{
    start();
  }, [])

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

I know it will work without start function in dependencies array. But it should be there according to React rules.
Maybe it could be a good thing to add as a prop in timer settings, like

const { time, start, pause, reset, isRunning } = useTimer({
    autostart: true // default: false
});

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

It's a good idea to add autostart feature.
I don't think it's an issue to have an empty dependency array: that way, you are sure to enter inside just once. More and more you fill it, and more and more you must rely on external dependencies (in our case, my library), for which functions identity could not be stable and cause some re-renders.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

If you’ll add autostart feature, there should also be an option to disable or enable it when component is already working. For example:

  1. Component mounted
  2. Timer starts
  3. Timer finished
  4. User clicks “reset timer“...
    What will be next? Timer will start again by default? Maybe after resetting timer I want to disable autostart.

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

In my opinion, since the timer has been auto-started once, it should not be auto-started again by default, after a reset for example.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

Fair enough

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

You want to make a PR for this auto-start feature? I you don't, i could make it quickly tonight.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

I think you will do it faster 😊

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

Hello @dancheskus ,

I made two PR:

  • One to fix the issue about start function: #50
  • An other to add autostart feature: #48

Please, have a look on it. If it's OK for you, I will include it inside release 2.0.0 and will close this issue.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

Thanks, I will check it today. The only thing I don't know how to test it with my React project. How can I download not master use-timer package but another branch?

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

If you select develop branch, for example (https://github.com/thibaultboursier/use-timer/tree/develop), and you download zip, you will have use-timer-develop.zip.
But i suppose you talk about NPM download? You can do that:

"dependencies": {
   "use-timer": "thibaultboursier/use-timer#develop"
}

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

It should work with my PR branches ;-)

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

Do you have some errors?

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

Do you have some errors?
step by step.

  1. yarn remove use-timer
  2. yarn add thibaultboursier/use-timer#fix-start-function-reference-issue
  3. result in dependencies:
  "dependencies": {
    "use-timer": "thibaultboursier/use-timer#fix-start-function-reference-issue"
  },
  1. yarn start
  2. error:
Failed to compile.

./src/components/MyComponent.js
Module not found: Can't resolve 'use-timer' in 'E:\Desktop\MyProj\src\components\MyComponent'

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

I'm going to test on my side.

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

Download module has empty source.

Capture d’écran 2020-10-22 à 00 25 36

If you want to find a fasted solution, maybe you can copy library code inside your project (if you are in TypeScript) to test it. I'll find a solution to have an elegant way to test branches.

from use-timer.

dancheskus avatar dancheskus commented on May 30, 2024

#48 looks good. Just waiting for variable rename

from use-timer.

thibaultboursier avatar thibaultboursier commented on May 30, 2024

Fixed by PR #50.

from use-timer.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.