I want to add a value in one of my state variables every second and show it on UI. I am trying the below code, but I am seeing weird behavior on UI(UI element is not updating every second correctly). I think because set state is async, it is taking time to update the state which is causing weird behavior. How can I achieve this?
const [abc, setAbc] = useState("");
const xyz = "aaa";
useEffect(() => {
setInterval(() => {
setAbc((prev) =>
prev.length !== xyz.length ? prev + xyz[prev.length] : ""
);
}, 1000);
});