Background
I'm making timer using React-Hook and Functional Component. The code below is working well.
type TProps = {
startTime: string;
lapTime: number;
};
export default function RemainTime({ lapTime }: TProps) {
const [lt, setLapTime] = useState<number>(lapTime);
useEffect(() => {
console.log('lapTime is changed');
const timer = setInterval(() => {
console.log('setInterval is called');
setLapTime((prev) => prev - 1);
}, 1000);
return () => {
console.log('component unmounted');
clearInterval(timer);
};
}, [lapTime]);
return <div>Remain Time : {lt}</div>;
}
The lt
changes every second.
Problem
Even though I re-render the RemainTime
component with new props({ lapTime: 300 }
), the lt
has a previous value.
What I want
I want the RemainTime
component to render new time when the lapTime
is changed.