This may be confusing but I appreciate the help. I essentially have a list of data with a "Copy" button next to each one. On click I want it to change to "Copied!" for 500 milliseconds and then go back to "Copy".
My setup is as follows: I have an array with the list of data that I've passed as a prop into a component (lets say array x).
Using this array I create a useEffect hook (that updates when the data array x is updated). This hook updates a new state with the new array (lets say array y) of the length of the original array with the text "Copy".
Seems to work great as far as I can tell, now heres where my problem is: I then create a handler for the button where I create a new array, using the dot notation spread and update the index of the button from "Copy" to "Copied!". Then a time out puts it back to "Copy" (code below).
Works great, but as you suspect I have a bug. If I hit copy on another button too quickly, I end up with the original "copied" button stuck there. As I'm guessing I'm updating the state faster then the other state drops back.
function copyButtonClipBrd(index) {
console.log("Test Copy Button");
console.log(cpyBTN);
let newArr = [...cpyBTN];
newArr[index] = "Copied!";
setCpyBTN(newArr);
setTimeout(function () {
let newArr = [...cpyBTN];
newArr[index] = "Copy";
setCpyBTN(newArr);
}, 600);
}
Would love any advice, or even better ways to do this as I'm largely doing this for learning. Thanks all!