本文共 808 字,大约阅读时间需要 2 分钟。
1.useEffect不带第二个参数
// Similar to componentDidMount and componentDidUpdate: useEffect(() => { // Update the document title using the browser API document.title = `You clicked ${count} times`; });
例子
import React, { useState, useEffect } from 'react';import { Button } from '@alifd/next';export default function Example() { const [count, setCount] = useState(0); // Similar to componentDidMount and componentDidUpdate: // 加载和state更新的时候会触发 useEffect(() => { console.log(`hook-----${new Date()}`); console.log(`count ${count} times`); // Update the document title using the browser API document.title = `You clicked ${count} times`; }); return ();}You clicked {count} times
当组件第一次被加载进来的时候,控制台打印
当每次点击按钮'cclick me',控制台打印
转载地址:http://euvm.baihongyu.com/