I'm trying to make an countdown website for a project. I want that the countdown is triggered by a button. I'm using React but i keep getting this error
React Hook "useEffect" is called in function "countdown" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter
I'm pretty new to react, can somebody give me some advice to fix the problem ?
the code:
import './App.css';
import React, { useEffect, useState } from 'react';
function App() {
const time = 20;
const [count, setCount] = useState(time);
const startCount = () => {
useEffect(() => {
if (count > 0) {
setTimeout(() => setCount(count - 1), 1000);
} else {
setCount('Times up');
}
});
};
return (
<div className="App">
<div>{count}</div>
<button onClick={startCount}> start </button>
<button> pauze </button>
</div>
);
}
export default App;
useEffectenables you to run something whenever the the component is rendered or when a state changes. Having that inside of a function that is called on click makes it useless.useEffectshould call other functions, but should never be called from within a function.