0
function App() {
  return <Store></Store>;
}
export function Store() {
    console.log("render Store");
    useState(()=>{
        console.log("useState")
    })
    useEffect(()=>{
        console.log("mount");
        return ()=>{ console.log("unmount");};
    },[])
    return <div>??</div>
}

The console will logrender Store=>useState=>render Store=>useState=>mount. Lazy load of useState run 2 time,Store rerender without any reason. enter image description here

It's very confused.

BeanBro
  • 51
  • 1
  • 6
  • I find the same question https://stackoverflow.com/questions/54927622/usestate-do-double-render ,the devto – BeanBro Mar 29 '20 at 09:54

2 Answers2

0

Try to wrap your component in StrictMode

<React.StrictMode>
    <Store/>
</React.StrictMode>
Max Starling
  • 967
  • 8
  • 8
0

React hooks run on each render cycle. State was initialized to `undefined and triggered second render (render phase), then component was mounted (commit phase).

enter image description here

Granted this diagram uses the class-based component's lifecycle functions, but the process is the same for functional components.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181