P粉5534287802023-08-15 00:58:02
您的useEffect
不会运行,因为users[index]
的值相同
将其更改为index
let users = ['Oliver', 'Thomas', 'George', 'William'] export default function App() { const [index, setIndex] = useState(0); console.log('RENDER'); useEffect(() => { if (index === users.length - 1) { return } setTimeout(() => setIndex(index => index + 1), 2000) console.log('Hello ' + users[index]); console.log('Side Effect RUNS!'); }, [index]) // 更改为 `[index]` }