Hook
是React 16.8.0版本增加的新特性/新语法- 可以在函数组件中使用 state 以及其他的 React 特性
this
HOOKS
。Hook
是React 16.8.0版本增加的新特性/新语法React.useState()
React.useEffect()
React.useRef()
使函数组件也可以有
state
状态, 并进行状态数据的读写操作。
import { useState } from "react";const [state, setState] = useState(initState);
initState
: useState 参数state
值在内部作缓存[state, setState]
: useState 返回值,包含两个元素的数组,state
状态值,state
状态值的函数第一种写法
setState(newState);
参数为 非函数值,直接指定新的状态值,内部用其覆盖原来的状态值
第二种写法
setState(state => {// state : 原本的state值return newState
});
参数为函数,接收原本的状态值,返回新的状态值,内部用其覆盖原来的状态值
const [count, setCount] = useState(0);setCount(count + 1);setCount((count) => {// count : 原本的state值return count + 1;
});
在函数组件中执行副作用操作(用于模拟类组件中的生命周期钩子)
可以用来更好的处理副作用,比如 添加订阅、设置定时器、接口请求 以及执行其他包含副作用的操作
import { useEffect } from "react";useEffect(() => {// 在此可以执行任何带副作用操作return () => { // 在组件卸载前执行// 在此做一些收尾工作, 比如清除定时器/取消订阅等};},[stateValue],// 如果指定的是[], 回调函数只会在第一次render()后执行
);
可以把 useEffect Hook 看做如下三个函数的组合:
componentDidMount()
componentDidUpdate()
componentWillUnmount()
const [count, setCount] = useState(0);
useEffect(() => {// 定时器let timer = setInterval(() => {setCount((count) => count + 1);}, 1000);return () => {// 清除定时器clearInterval(timer);};
}, []);
可以在函数组件中存储/查找组件内的标签或任意其它数据
作用:保存标签对象,功能与React.createRef()
一样
import { useRef } from "react";const refContainer = useRef(initValue);
myRef} type="text" />
const myRef = useRef();function showInputValue() {console.log(myRef.current.value);
}
类组件 对比 函数组件
实现 state 状态修改、定时器、输入框信息打印、组件卸载
类组件 实现
src/index.js 暴露 root
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { BrowserRouter } from "react-router-dom";const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
);
export default root;
import React, { Component } from "react";
import { createRef } from "react";
import root from "../../index";//引入root路径
export default class index extends Component {state = {count: 0,name: "tom",};myRef = createRef();componentDidMount() {this.timer = setInterval(() => {this.setState({count: this.state.count + 1,});}, 1000);}componentWillUnmount() {clearInterval(this.timer);}increment = () => {this.setState((state) => {return {count: state.count + 1,};});};updateName = () => {this.setState({name: "小明",});};unMount = () => {root.unmount();};showInputValue = () => {console.log(this.myRef.current.value);};render() {return (数值为:{this.state.count}
名字:{this.state.name}
this.myRef} type="text" />);}
}
函数组件 实现
src/index.js 暴露 root
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { BrowserRouter } from "react-router-dom";const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
);
export default root;
import { useState, useEffect, useRef } from "react";
import root from "../../index";//引入root路径
function Index() {const [count, setCount] = useState(0);const [name, setName] = useState("tom");const myRef = useRef();useEffect(() => {let timer = setInterval(() => {setCount((count) => count + 1);}, 1000);return () => {clearInterval(timer);};}, []);function increment() {// setCount(count + 1);// 第一种写法setCount((count) => {return count + 1;});}function updateName() {setName("小明");}function unMount() {root.unmount();}function showInputValue() {console.log(myRef.current.value);}return (数值为:{count}
名字:{name}
myRef} type="text" />);
}export default Index;
组件实现效果图