React Hook

React 声明组件的方式

Class声明和函数式声明(无状态组件)。
Class声明可以操作state、生命周期等
函数式声明不可以

Hook 是 React 16.8 的新增特性。它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性。

原则

· 只在最顶层调用Hook

· 只在React函数中调用Hook

state Hook

如下组件

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

  render() {
    return (
      

You clicked {this.state.count} times

); } }

可以写成

import React, { useState } from 'react';

function Example() {
  // 声明一个叫 "count" 的 state 变量
  const [count, setCount] = useState(0);

  return (
    
// 获取 state

You clicked {count} times

// 改变 state
); }

effect Hook

如下代码

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

  componentDidMount() {
    document.title = `You clicked ${this.state.count} times`;
  }

  componentDidUpdate() {
    document.title = `You clicked ${this.state.count} times`;
  }

  render() {
    return (
      

You clicked {this.state.count} times

); } }

可以写成

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

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

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

  return (
    

You clicked {count} times

); }

生命周期

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

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

  render() {
    return (
      

You clicked {this.state.count} times

{ this.state.count < 4 ? : null }
); } } function Example() { const [count, setCount] = useState(0); // Similar to componentDidMount and componentDidUpdate: useEffect(() => { // Update the document title using the browser API document.title = `You clicked ${count} times`; return function () { document.title = `React App` }; }); return (

You clicked {count} times

); } export default App;

组件的每一次更新都要调用Effects

创建时:调用Effects中的回调

更新时:先调用Effects中回调的return的回调,再调用Effects中的回调

卸载时:调用Effects中回调的return的回调

自定义Hook , 逻辑的抽离

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



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

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    console.log(1)
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });
  return { setCount, count };
}
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  render() {
    return (
      

You clicked {this.state.count} times

{ this.state.count < 4 ? : null } { this.state.count < 4 ? : null }
); } } function Example() { const { count, setCount } = useCount() return (

You clicked {count} times

); } function Example1() { const { count, setCount } = useCount() return (

You clicked {count} times

); } export default App;

· 自定义 Hook 必须以 “use” 开头
· 自定义 Hook被不同的组件使用时state和effects是独立的

你可能感兴趣的:(React Hook)