I have been learning react recently, and I created a component in which I have created three buttons: "post", "user", and "comment". When I click one of the buttons, the name of the button shows below the buttons and also logs the button's name, which is console. I am confused as to why a button logs the name only twice in console if clicked consecutively, since from my understanding it should log only once. I tried removing <React.StrictMode> from the index.js file but still it only logs twice. Example: if the user button is clicked twice consecutively, it logs its name twice but not more if clicked again, it should log into the console only once. I think it is related to the useState hook. Please help me understand this react behaviour. Here is my code:
export default function Hello() {
const [resourceType, setResourceType] = useState('post')
console.log(resourceType)
return (
<div>
<button onClick={() => setResourceType('post')}>Post</button>
<button onClick={() => setResourceType('user')}>User</button>
<button onClick={() => setResourceType('comment')}>Comments</button>
<h1>{resourceType}</h1>
</div>
)
}