I got rid of the issue by assigning the index to another variable inside the local function function then using that local variable instead of the original index.
const i = index setDisplayText((prevText) => prevText + text[i]);