React Hook - 对比Class组件理解学习

本文详细介绍了React Hooks的使用,包括useState、useEffect、useContext、useReducer、useRef等,通过对比Class组件,阐述了Hooks如何简化React应用的开发,特别强调了Hook的规则和最佳实践,如使用exhaustive-deps规则,并提供了TodoList的useReducer+useContext实现。
摘要由CSDN通过智能技术生成

React Hook 版本及使用规则

版本React 16.8及以上

使用规则:

  • 只能在函数最外层调用Hook。不要在循环、条件判断或者子函数中调用。(React依靠hook的调用顺序来确定哪个state对应哪个hook。因此在写代码的过程中,要保证hook的调用顺序在每次渲染中都是相同的。确保它能正常运行)
  • 只能在 React 的函数组件中调用 Hook。 在自定义 Hook 中调用其他 Hook.不要在其他 JavaScript 函数中调用。

useState:

const [xxx, setxxx] = useState(initialState)

Class组件写法

import React, {
    Component } from 'react';

class Example extends Component{
   
    constructor(){
   
        super();
        this.state = {
   
            count: 0
        }
    }

    render() {
   
        return (
            <div>
                <h2>You clicked {
    this.state.count } times</h2>
                <button onClick={
   () => {
   this.setState({
   count: this.state.count + 1})}}>Click me</button>
            </div>
        );
    }
}

export default Example; 

Hook写法

import React, {
    useState } from 'react';

function Example() {
   
    const [count, setCount] = useState(0);

    return(
        <div>
            <h2>You clicked {
    count } times</h2>
            <button onClick={
   () => {
   setCount(count + 1)}}>Click me</button>
        </div>
    );
}

export default Example;

知识点总结:

  • useState
    只有唯一的参数,用于给state初始化赋值。两个返回值,一个是state,一个是用于更新state的函数(可以理解为每个state有了自己的this.setState.).
    通常我们使用数组解构赋值来获取这两个返回值, 例如: const [count, setCount] = useState(0);
  • useState需成对出现,需要多少state和对应的更新state函数,就声明多少次。
  • React依靠hook的调用顺序来确定哪个state对应哪个useState。因此在写代码的过程中,要保证hook的调用顺序在每次渲染中都是相同的。确保它能正常运行
  • useState不会自动合并更新对象,需要用函数式的方式来达到效果
setCount(prevCount => {
   
  // 也可以使用 Object.assign
  return {
   ...prevCount, ...updatedValues};
});
  • state初始化参数既可以是值,也可以是一个函数(返回初始的state)。此函数只在初始渲染时被调用。
const [count, setCount] = useState(() => {
   
  const initialCount = someComputation(props);
  return initialCount;
});

useEffect

effect分类

无需清除的effect
比如发送网络请求,手动变更 DOM,记录日志、

需要清除的effect
例如订阅外部数据源,监听器,定时器

无需清除的effect Class组件写法

import React, {
   Component} from 'react';

class Example extends Component{
   
    constructor(props){
   
        super(props);
        this.state = {
   
            count: 0
        }
    }

    componentDidMount(){
   
        console.log(`You clicked ${
     this.state.count} times`);
    }

    componentDidUpdate(){
   
        console.log(`You clicked ${
     this.state.count} times`);
    }

    render(){
   
        return(
            <div>
                <h2>You clicked {
   this.state.count} times</h2>
                <button onClick={
   () => {
   this.setState({
    count: this.state.count + 1})}}>Click me</button>
            </div>
        );
    }
}

export default Example;

无需清除的effect Hook

import React, {
    useState, useEffect } from 'react';

function Example() {
   
    const [count, setCount] = useState(0);

    useEffect(()=>{
   
       console.log(`You clicked ${
     count} times`);
    });

    return(
        <div>
            <h2>You clicked {
   count} times</h2>
            <button onClick={
   () => {
   setCount(count + 1)}}>Click me</button>
        </div>
    );
}

export default Example;

需清除的effect Class组件写法

import React, {
   Component} from 'react';

class Example extends Component{
   
    constructor(props){
   
        super(props);
        this.state = {
   
            innerWidth: 0,
            innerHeight: 0
        };
        this.handleWindowSizeChange = this.handleWindowSizeChange.bind(this);
    }

    componentDidMount(){
   
        window.addEventListener('resize', this.handleWindowSizeChange);
    }

    componentWillUnmount(){
   
        window.removeEventListener('resize', this.handleWindowSizeChange);
    }

    handleWindowSizeChange(){
   
        this.setState({
   
            innerWidth: window.innerWidth,
            inner
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值