首页 > 其他分享 >react 函数组件和hook

react 函数组件和hook

时间:2024-04-08 22:15:33浏览次数:13  
标签:arr 函数 第二个 react hook 参数 组件 let

函数组件

1.函数组件没有生命周期 2.函数组件没有this 3.函数组件通过hook完成各种操作 4.函数组件本身就是render函数 5.props在函数第一个参数解释 useState 参考https://www.cnblogs.com/ssszjh/p/14581014.html props 参考https://www.cnblogs.com/ssszjh/p/18118746 生命周期 useEffect  相当于监听属性 参考https://www.cnblogs.com/ssszjh/p/18122682 useMemo相当于计算属性,可以缓存变量 1.不传第二个参数相当于初始化和更新的时候都会执行, componentDidMount 和 componentDidUpdate 2.第二个参数[],里面没有值表示只在初始化时执行一次相当于componentDidMount 3.第二个参数有值表示只有当arr变化时,才去再次执行 useCallbac缓存方法 useEffect useMemo useCallbac第二个参数都是一样的 useRef和useContext 参考https://www.cnblogs.com/ssszjh/p/18122747  
import styles from './index.less';
import { Button } from 'antd';
import React, { useCallback, useMemo, useState } from 'react';
const HomePage: React.FC = () => {
  let [arr, setArr] = useState([1, 2, 3])
  let [str, setStr] = useState('sqweqwe')
  // <div>1.不传第二个参数相当于初始化和更新的时候都会执行, componentDidMount 和 componentDidUpdate</div>
  // <div>2.第二个参数[],里面没有值表示只在初始化时执行一次相当于componentDidMount</div>
  // <div>3.第二个参数有值表示只有当arr变化时,我们才去再次执行</div> 
  let all = useMemo(() => {
    console.log(1)
    let _all = 0
    arr.forEach(item => {
      _all += item
    })
    return _all
  }, [arr])
  let changeMes = useCallback(() => {
    setStr(str + '1')
  }, [])
  function changeArr() {
    let newArr = [...arr]
    newArr.push(1)
    setArr(newArr)
  }
  return (
    <div className={styles.container}>
      <h2>函数组件和hook</h2>
      <h3>函数组件</h3>
      <div>1.函数组件没有生命周期</div>
      <div>2.函数组件没有this</div>
      <div>3.函数组件通过hook完成各种操作</div>
      <div>4.函数组件本身就是render函数</div>
      <div>5.props在函数第一个参数解释</div>
      <div>useState 参考test3</div>
      <div>props 参考test5</div>
      <div>生命周期 useEffect 参考test6 相当于监听属性</div>
      <div>useMemo相当于计算属性</div>
      <div>1.不传第二个参数相当于初始化和更新的时候都会执行, componentDidMount 和 componentDidUpdate</div>
      <div>2.第二个参数[],里面没有值表示只在初始化时执行一次相当于componentDidMount</div>
      <div>3.第二个参数有值表示只有当arr变化时,我们才去再次执行</div>
      <div>减少子组件的更新</div>
      <div>{str}</div>
      <Button type="primary" size="small" onClick={changeMes}>修改字符串数据</Button>
      <br></br>
      <Button type="primary" size="small" onClick={changeArr}>修改数组数据</Button>
      <div>{all}</div>
      <div>useCallbac缓存方法</div>
      <div>useEffect useMemo useCallbac第二个参数都是一样的</div>
      <div>useRef和useContext 参考test7</div>
    </div>
  );
};



export default HomePage;

 

标签:arr,函数,第二个,react,hook,参数,组件,let
From: https://www.cnblogs.com/ssszjh/p/18122752

相关文章

  • react 性能问题和优化
    某个组件更新,子组件也会一起更新react更新采用时间切片,vue则是依赖收集执行更新操作为16ms,如果操过16ms,先暂停更新,让浏览器先渲染时间切片时间是16ms,因为人眼刷新率约60帧,60hz为16ms1.避免state改为同样的值(class用PureComponent,函数组件默认已经处理了)2.处理子组......
  • react 生命周期
    1.class组件初次挂载1.constructor2.getDerivedStateFromProps3.render4.componentDidMount 更新数据1.getDerivedStateFromProps2.shouldComponentUpdate3.render4.componentDidUpdate备注:1.PureComponent里不能写shouldComponentUpdate2.优化性能一般在shoul......
  • Django框架之form组件
    一、前戏需求:写一个注册功能获取用户名和密码,利用form表单提交数据在后端判断用户名和密码是否符合一定的条件用户名不能含有三国演义密码不少于三位1、前端<formaction=""method="post"><p>username:<inputtype="text"name="username"><......
  • react中redux基本使用二
    1.action传参,用payload属性接收  <buttononClick={()=>dispatch(addNum(2))}>+2</button> 2.redux中异步操作,与同步类似,需要比同步多封装一个函数//使用RTK创建store,createSlice创建reducer的切片//使用RTK创建store,createSlice创建reducer的切片import......
  • vue 全局组件 局部组件
    全局组件:<script>//创建vue实例constapp=Vue.createApp({template:`<div><hello/><world/><hello-world/></div>`});//子组件//组件具备复用性//全局组件,只要定义了,处处可以使用,性能不高,但是使用起来简单......
  • Blazor学习记录_12._IIS部署_组件的引用_
    27.Blazor项目发布与IIS部署27.1如果是Auto模版的项目,选择两个项目中的Server项目进行发布27.2服务器必要的运行时安装与配置1.安装运行时可先通过命令行输入:dotnet--info来查看本地已经安装的运行时情况。运行时官方下载页面:https://dotnet.microsoft.com/zh-cn/dow......
  • react中redux基本使用
    1.安装 npminstall@reduxjs/toolkitreact-redux2.创建store目录 3.创建counterStore.js//使用RTK创建store,createSlice创建reducer的切片import{createSlice}from"@reduxjs/toolkit";constcounterSlice=createSlice({//需要一个配置对象作为参数,通过......
  • 前端【Vant】01-移动端组件库
    1、介绍Vant是一个轻量、可靠的移动端组件库,于2017年开源。目前Vant官方提供了 Vue2版本、Vue3版本和微信小程序版本,并由社区团队维护 React版本和支付宝小程序版本。2、安装1#Vue3项目,安装最新版Vant:2npmivant-S34#Vue2项目,安装Vant......
  • Vite+Vue3.0项目使用ant-design-vue <a-calendar>日期组件汉化
    antd的弹框、日期等默认为英文,要把英文转为中文请看下文:1.首先我们要在main.js中引入ant-design组件库并全局挂载:importAppfrom'./App'importAntdfrom'ant-design-vue';import'ant-design-vue/dist/antd.css';constapp=createApp(App);app.use(Antd);2.然......
  • vue3使用van-picker组件时,滑动选中某个数值时在旁边相应显示单位
    vant的官方文档中没有支持这种需求的属性或方法,所以只能自己找别的方法。方法一van-picker控件选中某个元素时,该元素会有一个类名van-picker-column__item–selected我们可以在css里给这个类名通过::after,即使用伪类,来添加单位.van-picker-column__item--selected::aft......