• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to use input from user to countdown?

Resolved: How to use input from user to countdown?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m making a countdown and I want that the user can say to which date the countdown should go. I already created an input from the user and I guess I should change the const difference to use the date from the input but I don’t know how I could do that.

Answer:

As I mentioned on my comment, every input has a onChange prop in which you can get updates when the value changes.
So you can create a new state variable that will hold the target date, and call its setter when that event happens:
I think this should work. A few things worthy of note:
  1. value={formatDate(date)} on the input is not always needed, but on yours it is, because you want to show the initial date which is a specific one – Check for “Controlled vs uncontrolled components”
  2. I had to format the date into a yyyy-mm-dd string because it’s what the input type="date" requires for its value. But your state is of type Date, we just format it when we’re about to pass it to the input.
  3. There’s a different way of handling this altogether. timeLeft is in essence a derived state of date, but you need to rerender the component every second. You could then avoid having a state for timeLeft, and just calculate it whenever the component renders. And have something that forces a rerender every second. It’s a bit more advanced and there are devs that don’t really like this method, so don’t worry too much about it.

Hope that helps!

If you have better answer, please add a comment about this, thank you!

reactjs
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to get Union type from an array of objects in Flow?

24/03/2023

Resolved: Modify entity using Action in C#

24/03/2023

Resolved: How to give rank on datetime column group by another column with userid in it

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.