There seems to be a strange issue in my React TS project - I have to click the button twice to switch the text from "Goodbye" to "Hello", but not the other way around. Any ideas why?
import { useState } from 'react'
const ChangeTextButton = () => {
const [buttonText, setButtonText] = useState("Click me!")
var changedBefore = false;
function changeText() {
if (changedBefore) {
setButtonText("Hello")
} else {
setButtonText("Goodbye")
}
changedBefore = !changedBefore
return buttonText;
}
return(
<button onClick={() => changeText()}>{buttonText}</button>
);
}
export default ChangeTextButton;
Seems like there might be some issues with this code, but I'm still learning TypeScript and JS.