I starting to learn React and I was wondering why my Counter it's not working :( My objective is: When you press the button, Counter function start to "count" and render "count" state in the td. I don't know what I'm doing wrong.
import React, { useState } from "react";
function Clock() {
const customStyle = {
fontSize: "100px"
};
const tdStyle = "px-3 border border-dark border-3 rounded bg-secondary";
//-------------------------------------------------------------------------
// THIS FUNCTION
const [count, setCount] = useState(0);
function Counter(){
setCount(count + 1)
setInterval(Counter, 1000)
}
return (
<div className="rounded border border-dark border-3">
<table>
<tr style={customStyle}>
<td className={tdStyle}>
<i className="fa-solid fa-clock"></i>
</td>
<td className={tdStyle}>0</td>
<td className={tdStyle}>0</td>
<td className={tdStyle}>0</td>
<td className={tdStyle}>0</td>
<td className={tdStyle}>0</td>
<td className={tdStyle}>{count}</td> //here is showed
</tr>
</table>
<button onClick={Counter}>Pulsar</button> //button
</div>
);
}
export default Clock;
This works with real seconds
function Counter(){
setCount(new Date().getSeconds())
setInterval(Counter, 1000)
}
But it is not what I want. I want a simple counter.
In addition, How could I use "onLoad" event in this code? for when page is loaded It start counting and rendering it in the page?
Thank you in advance!!