hook钩子介绍
发布日期:2021-05-06 19:34:58 浏览次数:11 分类:技术文章

本文共 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',控制台打印

 

上一篇:SASS—混合指令 (Mixin Directives)
下一篇:浅谈什么是前端SEO

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月26日 01时14分53秒