react Hooks useEffect

news/2024/7/15 19:34:18 标签: react.js

useEffect可以相当于三个生命周期钩子 页面加载 组件更新 页面卸载
在这里插入图片描述

import './App.css';
import React, { useState } from 'react'
import ReactDOM from 'react-dom'
function App() {
  const [count, setcount] = useState({
    a: 0,
    b: 3
  })
React.useEffect(()=>{
  console.log("11111");
  return()=>{
    console.log("222");
  }
},[])
function add(){
  setcount({
    ...count,
    b:count.b+1
  })
}
function unmount(){
  ReactDOM.unmountComponentAtNode(document.getElementById("root"))
}
  return (
    <div id="App">
      <div></div>
      <p id="a">{count.b}</p>
      <button onClick={add}>按钮</button>
      <button onClick={unmount}>按钮</button>
    </div>
  );
}
export default App;

在这里插入图片描述
在这里插入图片描述


http://www.niftyadmin.cn/n/1371695.html

相关文章

react hooks useRef

useRef可用于保存遍量和用作ref import ./App.css; import React from react function App() {const inputrefReact.useRef(null);const save React.useRef({value:"程序员"})return (<div id"App"><input type"text" placeholder&qu…

reacthooks useMemo用于组件状态更新时在页面上不更新渲染

状态是改变的&#xff0c;在页面上阻止他改变&#xff0c;页面上并不改变

Git报错 Incorrect username or password (access token) 的解决方式

错误原因 在使用git的时候 出现 Incorrect username or password (access token)&#xff0c;这个报错主要就是代表本地保存的gitee或者GitHub的账号还有密码错误。而他们这些账号密码都保存到了windows的凭据管理器 解决方式 首先打开windows的凭据管理器 凭据管理器所在的…

reacthooks useCallback

[ ]里面为空时不执行函数&#xff0c;里面有数组时&#xff0c;数组里面的变量改变时就执行usecallcabk里面的函数

react Hooks usecontext 和useuseReducer 完成小案例

show.jsx import React, { useContext } from "react"; import { Colorcontext } from "../App.js" function A() {const count useContext(Colorcontext)return (<div style{{color:count}}>我是文字&#xff0c;我现在变成了{count}</div>…

复习redux 和react-redux

redux redux分为action和reducer和store 其中store是核心是连接他们的桥梁&#xff0c;store不需要自己写&#xff0c;用{ }的形式在“redux”中导入createStore函数就行了 然后直接默认导出他export default createStore()里面是reducer 一般是都吧所有的reducer整合成一个然后…