I am trying to implement a countdown, and I just cannot grasp the basics of React.
React.useState() works if I use a hard-coded value, but if I try to pass something from the app, nothing displays.
modelUpdate() works if I use a hard-coded value when I update, but not if I try to calculate the next number.
I am open to any alternate implementation, but the below shows what I have tried, and what works and what doesn't. I don't know if I just can't figure out the syntax or what.
////////////////////////////////////////////////
const MyCustomComponent = ({ triggerQuery, model, modelUpdate }) => {
// HARD-CODED STARTING VALUE WORKS
const [timerId, setTimerId] = React.useState(0)
// INITIALIZATION BASED ON PASSED-IN VALUES **DOESN'T** WORK
//const [myVar, setMyVar] = React.useState({model.var1})
function scheduledUpdate() {
// HARD-CODED UPDATE WORKS
modelUpdate({var1: 2})
// CALCULATED UPDATE **DOESN'T** WORK
//modelUpdate({var1: var1 + 1})
// DIFFERENT SYNTAX FOR CALCULATED UPDATE ALSO **DOESN'T** WORK
//moduleUpdate({var1: {var1} + 1})
//moduleUpdate({var1: {model.var1} + 1})
// THIS CALCULATED UPDATE DOES WORK
setTimerId( timerId + 1 )
}
React.useEffect(() => {
setInterval( scheduledUpdate, 1000 );
}
return (
<div className="card">{timerId}</div>
);
}
////////////////////////////////////////////////
Furthermore, the timer that counts up in my example, seems to scroll through all possible values until it lands on the latest value. I don't understand that either. Am I not using React correctly regarding the setState functionality?