The function does not run again as the state remains the same as it should in the first case.
Works as expected, I have no issues with it.
import {useState, useEffect} from 'react'
export default function App(){
const [name, setName] = useState("Joe")
console.log("render")
return (
<div>
<h1>{name}</h1>
<button onClick={()=>{setName("Joe")}}>
Change Name
</button>
</div>
)
}
In the first case, the output after pressing the Change Name button: first code render nothing happened. You can just see the first render text, there is no second but in the second case the situation is slightly different.
import {useState, useEffect} from 'react'
export default function App(){
const [name, setName] = useState("Joe")
console.log("render")
return (
<div>
<h1>{name}</h1>
<button onClick={()=>{setName("Jane")}}>
Change Name
</button>
</div>
)
}
After the first render i press the button and the state changes to Jane and the component is rendered again as it should. second code second render
but after that when i press the button again component should not be called again because state has not changed. However, the component is called again and rendering takes place again.
After that, when I press the button again, the component is not called again and does not work as it should.
My question is: Why component is called again even though the state is the same? Why did it work differently in the second case, although in the first case it was not called again when the state was the same?
I tried to debug etc. and couldn't find anything helpful.
Thanks for all answers.