if i put onClick = {console.log("First button clicked)} inside NavDropdown.Item/> it outputs on console First button clicked without even clicking it. ??? Why ?? .
How can i use the onClick property to change some state ?
This behaviour is the same reason that, when you add a callback function to an element, you pass in the declaration, and not the function call. Example:
const myComp = () => {
const [myState, setMyState] = React.useState(0);
const callbackFunc = () => {
console.log("Logged");
setMyState(2);
}
return(
<div>
<button onClick={callbackFunc}>Click me to log</button>
<button onClick={callbackFunc()}>I have already invoked the callback</button>
</div>
)
}
This is covered in the React section of the www.freeCodeCamp.org/learn course. But the above example also includes this…