1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import React, { useState, useEffect } from 'react'; import './App.css'; function App() { const [counter, setCounter] = useState(0); console.log('App', 'start point'); useEffect(() => { console.log('useEffect', 'start point'); setTimeout(() => { console.log('setTimeout', 'setCounter to 1'); // first time, it sets the 0 to 1. So we call render. Math.random() runs // 2nd time, tries to set 1 to 1, no render gets called. No Math.random(). // https://en.reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update // If you update a State Hook to the same value as the current state, // React will bail out without rendering the children or firing effects. setCounter(1); }, 5000); }); // initially, counter is 0, then Math.random() is called return ( <div className="ticker" > <b>{Math.random()}</b> <div> <div className="ticker-name">{counter}</div> </div> </div> ); } export default App; |