why code leads to infinite loop even when value within setState() is not changing.. we know state do not change if the current value and previous values are same. yet state is changing which is leading the component to re-render.
** we know component re-render when state changes
but previous and current values are same
then why component re rendering
also object is not passed as argument where react checks on basis of reference. as this is a primitive react checks on basis of value. still why the component re rendering???
import React, { useState } from 'react'
import logo from './logo.svg'
import './App.css'
function App() {
const [state,setState] = useState(0)
console.log(state)
setState((prev)=>7)
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.jsx</code> and save to reload!
</p>
<span className="App-link">Hello from codedamn :)</span>
</header>
</div>
)
}
export default App
setState outside a useEffect totally not equals to useEffect without dependency array. you can try. setState(9) inside a useEffect with no dependency array provided. and setState(9) without useEffect as in the code.