react has a total of 9 hooks: useState, useEffect, useContext, useReducer, useCallback, useMemo, useRef, useImperativeHandle, useLayoutEffect.
The operating environment of this tutorial: Windows7 system, react17.0.1 version, Dell G3 computer.
In the world of React, there are container components and UI components. Before React Hooks appeared, we could use functions for UI components. Stateless components are used to display UI, but for container components, function components are powerless. We rely on class components to obtain data, process data, and pass parameters down to UI components for rendering. In my opinion, using React Hooks has the following advantages compared to previous class components:
The code is more readable, and the code logic of the same function is split In different life cycle functions, it is easy for developers to be disadvantaged in maintenance and iteration. Through React Hooks, functional codes can be aggregated to facilitate reading and maintenance.
The component tree level becomes shallower. In the original code, we often use HOC/render props and other methods to reuse component status, enhance functions, etc., which undoubtedly increases the number of component tree layers and rendering. In React Hooks, these functions can be powerfully customized Hooks to implement
React launched the new feature of React Hooks in version v16.8, although the community does not yet have best practices on how to build complex applications based on React Hooks (at least I don’t yet) No), relying on reading a large number of articles about this in the community, I will use ten cases to help you understand, understand and skillfully use most of the features of React Hooks.
In class components, we use this.state
to save component state and trigger modifications The component re-renders. For example, the following simple counter component is a good explanation of how class components operate:
import React from "react"; class App extends React.Component { constructor(props) { super(props); this.state = { count: 0, name: "alife" }; } render() { const { count } = this.state; return ( <p> Count: {count} <button onClick={() => this.setState({ count: count + 1 })}>+</button> <button onClick={() => this.setState({ count: count - 1 })}>-</button> </p> ); } }
A simple counter component is completed, and in function components, since there is no black magic like this, React uses useState to Help us save the state of the component.
import React, { useState } from "react"; function App() { const [obj, setObject] = useState({ count: 0, name: "alife" }); return ( <p className="App"> Count: {obj.count} <button onClick={() => setObject({ ...obj, count: obj.count + 1 })}>+</button> <button onClick={() => setObject({ ...obj, count: obj.count - 1 })}>-</button> </p> ); }
Returns an array with the default state and changing state functions by passing in the useState parameter. Change the original state value by passing the new state to the function. It is worth noting that useState does not help you handle the state. Compared with setState's non-coverage update state, useState's overlay update state requires developers to handle the logic themselves. (Code as above)
It seems that after useState, the function component can also have its own state, but this alone is not enough.
Function components can save state, but for asynchronous requests, side effects operations are still powerless, so React provides useEffect to help developers deal with the side effects of function components. Before introducing the new API, let's first take a look at how class components are done:
import React, { Component } from "react"; class App extends Component { state = { count: 1 }; componentDidMount() { const { count } = this.state; document.title = "componentDidMount" + count; this.timer = setInterval(() => { this.setState(({ count }) => ({ count: count + 1 })); }, 1000); } componentDidUpdate() { const { count } = this.state; document.title = "componentDidMount" + count; } componentWillUnmount() { document.title = "componentWillUnmount"; clearInterval(this.timer); } render() { const { count } = this.state; return ( <p> Count:{count} <button onClick={() => clearInterval(this.timer)}>clear</button> </p> ); } }
In the example, the component updates the component status every second, and each time the update is triggered, the update of document.title will be triggered ( side effect), and modify document.title when the component is unloaded (similar to clearing)
As you can see from the example, some repetitive functions developers need to write repeatedly in componentDidMount and componentDidUpdate, but if useEffect is used, it will be completely no the same.
import React, { useState, useEffect } from "react"; let timer = null; function App() { const [count, setCount] = useState(0); useEffect(() => { document.title = "componentDidMount" + count; },[count]); useEffect(() => { timer = setInterval(() => { setCount(prevCount => prevCount + 1); }, 1000); // 一定注意下这个顺序: // 告诉react在下次重新渲染组件之后,同时是下次执行上面setInterval之前调用 return () => { document.title = "componentWillUnmount"; clearInterval(timer); }; }, []); return ( <p> Count: {count} <button onClick={() => clearInterval(timer)}>clear</button> </p> ); }
We rewrote the above example using useEffect. The first parameter of useEffect receives a function, which can be used to do some side effects such as asynchronous requests, modifying external parameters and other behaviors, while the second parameter Called dependencies, it is an array. If the value in the array changes, the function in the first parameter of useEffect will be triggered to execute. The return value (if any) is called before the component is destroyed or the function is called.
Based on this powerful Hooks, we can simulate and encapsulate other life cycle functions, such as componentDidUpdate. The code is very simple
function useUpdate(fn) { // useRef 创建一个引用 const mounting = useRef(true); useEffect(() => { if (mounting.current) { mounting.current = false; } else { fn(); } }); }
Now we have useState management state, useEffect handles side effects and asynchronous logic. Learning these two tricks is enough to handle the usage scenarios of most class components.
上面介绍了 useState、useEffect 这两个最基本的 API,接下来介绍的 useContext 是 React 帮你封装好的,用来处理多层级传递数据的方式,在以前组件树种,跨层级祖先组件想要给孙子组件传递数据的时候,除了一层层 props 往下透传之外,我们还可以使用 React Context API 来帮我们做这件事,举个简单的例子:
const { Provider, Consumer } = React.createContext(null); function Bar() { return <Consumer>{color => <p>{color}</p>}</Consumer>; } function Foo() { return <Bar />; } function App() { return ( <Provider value={"grey"}> <Foo /> </Provider> ); }
通过 React createContext 的语法,在 APP 组件中可以跨过 Foo 组件给 Bar 传递数据。而在 React Hooks 中,我们可以使用 useContext 进行改造。
const colorContext = React.createContext("gray"); function Bar() { const color = useContext(colorContext); return <p>{color}</p>; } function Foo() { return <Bar />; } function App() { return ( <colorContext.Provider value={"red"}> <Foo /> </colorContext.Provider> ); }
传递给 useContext 的是 context 而不是 consumer,返回值即是想要透传的数据了。用法很简单,使用 useContext 可以解决 Consumer 多状态嵌套的问题。
function HeaderBar() { return ( <CurrentUser.Consumer> {user => <Notifications.Consumer> {notifications => <header> Welcome back, {user.name}! You have {notifications.length} notifications. </header> } } </CurrentUser.Consumer> ); }
而使用 useContext 则变得十分简洁,可读性更强且不会增加组件树深度。
function HeaderBar() { const user = useContext(CurrentUser); const notifications = useContext(Notifications); return ( <header> Welcome back, {user.name}! You have {notifications.length} notifications. </header> ); }
useReducer 这个 Hooks 在使用上几乎跟 Redux/React-Redux 一模一样,唯一缺少的就是无法使用 redux 提供的中间件。我们将上述的计时器组件改写为 useReducer,
import React, { useReducer } from "react"; const initialState = { count: 0 }; function reducer(state, action) { switch (action.type) { case "increment": return { count: state.count + action.payload }; case "decrement": return { count: state.count - action.payload }; default: throw new Error(); } } function App() { const [state, dispatch] = useReducer(reducer, initialState); return ( <> Count: {state.count} <button onClick={() => dispatch({ type: "increment", payload: 5 })}> + </button> <button onClick={() => dispatch({ type: "decrement", payload: 5 })}> - </button> </> ); }
用法跟 Redux 基本上是一致的,用法也很简单,算是提供一个 mini 的 Redux 版本。
在类组件中,我们经常犯下面这样的错误:
class App { render() { return <p> <SomeComponent style={{ fontSize: 14 }} doSomething={ () => { console.log('do something'); }} /> </p>; } }
这样写有什么坏处呢?一旦 App 组件的 props 或者状态改变了就会触发重渲染,即使跟 SomeComponent 组件不相关,由于每次 render 都会产生新的 style 和 doSomething(因为重新render前后, style 和 doSomething分别指向了不同的引用),所以会导致 SomeComponent 重新渲染,倘若 SomeComponent 是一个大型的组件树,这样的 Virtual Dom 的比较显然是很浪费的,解决的办法也很简单,将参数抽离成变量。
const fontSizeStyle = { fontSize: 14 }; class App { doSomething = () => { console.log('do something'); } render() { return <p> <SomeComponent style={fontSizeStyle} doSomething={ this.doSomething } /> </p>; } }
在类组件中,我们还可以通过 this 这个对象来存储函数,而在函数组件中没办法进行挂载了。所以函数组件在每次渲染的时候如果有传递函数的话都会重渲染子组件。
function App() { const handleClick = () => { console.log('Click happened'); } return <SomeComponent onClick={handleClick}>Click Me</SomeComponent>; }
这里多说一句,一版把函数式组件理解为class组件render函数的语法糖,所以每次重新渲染的时候,函数式组件内部所有的代码都会重新执行一遍。所以上述代码中每次render,handleClick都会是一个新的引用,所以也就是说传递给SomeComponent组件的props.onClick一直在变(因为每次都是一个新的引用),所以才会说这种情况下,函数组件在每次渲染的时候如果有传递函数的话都会重渲染子组件。
而有了 useCallback 就不一样了,你可以通过 useCallback 获得一个记忆后的函数。
function App() { const memoizedHandleClick = useCallback(() => { console.log('Click happened') }, []); // 空数组代表无论什么情况下该函数都不会发生改变 return <SomeComponent onClick={memoizedHandleClick}>Click Me</SomeComponent>; }
老规矩,第二个参数传入一个数组,数组中的每一项一旦值或者引用发生改变,useCallback 就会重新返回一个新的记忆函数提供给后面进行渲染。
这样只要子组件继承了 PureComponent 或者使用 React.memo 就可以有效避免不必要的 VDOM 渲染。
useCallback 的功能完全可以由 useMemo 所取代,如果你想通过使用 useMemo 返回一个记忆函数也是完全可以的。
useCallback(fn, inputs) is equivalent to useMemo(() => fn, inputs).
所以前面使用 useCallback 的例子可以使用 useMemo 进行改写:
function App() { const memoizedHandleClick = useMemo(() => () => { console.log('Click happened') }, []); // 空数组代表无论什么情况下该函数都不会发生改变 return <SomeComponent onClick={memoizedHandleClick}>Click Me</SomeComponent>; }
唯一的区别是:**useCallback 不会执行第一个参数函数,而是将它返回给你,而 useMemo 会执行第一个函数并且将函数执行结果返回给你。**所以在前面的例子中,可以返回 handleClick 来达到存储函数的目的。
所以 useCallback 常用记忆事件函数,生成记忆后的事件函数并传递给子组件使用。而 useMemo 更适合经过函数计算得到一个确定的值,比如记忆组件。
function Parent({ a, b }) { // Only re-rendered if `a` changes: const child1 = useMemo(() => <Child1 a={a} />, [a]); // Only re-rendered if `b` changes: const child2 = useMemo(() => <Child2 b={b} />, [b]); return ( <> {child1} {child2} </> ) }
当 a/b 改变时,child1/child2 才会重新渲染。从例子可以看出来,只有在第二个参数数组的值发生变化时,才会触发子组件的更新。
useRef 跟 createRef 类似,都可以用来生成对 DOM 对象的引用,看个简单的例子:
import React, { useState, useRef } from "react"; function App() { let [name, setName] = useState("Nate"); let nameRef = useRef(); const submitButton = () => { setName(nameRef.current.value); }; return ( <p className="App"> <p>{name}</p> <p> <input ref={nameRef} type="text" /> <button type="button" onClick={submitButton}> Submit </button> </p> </p> ); }
useRef 返回的值传递给组件或者 DOM 的 ref 属性,就可以通过 ref.current 值访问组件或真实的 DOM 节点,重点是组件也是可以访问到的,从而可以对 DOM 进行一些操作,比如监听事件等等。
当然 useRef 远比你想象中的功能更加强大,useRef 的功能有点像类属性,或者说您想要在组件中记录一些值,并且这些值在稍后可以更改。
利用 useRef 就可以绕过 Capture Value 的特性。可以认为 ref 在所有 Render 过程中保持着唯一引用,因此所有对 ref 的赋值或取值,拿到的都只有一个最终状态,而不会在每个 Render 间存在隔离。
React Hooks 中存在 Capture Value 的特性:
function App() { const [count, setCount] = useState(0); useEffect(() => { setTimeout(() => { alert("count: " + count); }, 3000); }, [count]); return ( <p> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>增加 count</button> <button onClick={() => setCount(count - 1)}>减少 count</button> </p> ); }
先点击增加button,后点击减少button,3秒后先alert 1,后alert 0,而不是alert两次0。这就是所谓的 capture value 的特性。而在类组件中 3 秒后输出的就是修改后的值,因为这时候** message 是挂载在 this 变量上,它保留的是一个引用值**,对 this 属性的访问都会获取到最新的值。讲到这里你应该就明白了,useRef 创建一个引用,就可以有效规避 React Hooks 中 Capture Value 特性。
function App() { const count = useRef(0); const showCount = () => { alert("count: " + count.current); }; const handleClick = number => { count.current = count.current + number; setTimeout(showCount, 3000); }; return ( <p> <p>You clicked {count.current} times</p> <button onClick={() => handleClick(1)}>增加 count</button> <button onClick={() => handleClick(-1)}>减少 count</button> </p> ); }
只要将赋值与取值的对象变成 useRef,而不是 useState,就可以躲过 capture value 特性,在 3 秒后得到最新的值。
通过 useImperativeHandle 用于让父组件获取子组件内的索引
import React, { useRef, useEffect, useImperativeHandle, forwardRef } from "react"; function ChildInputComponent(props, ref) { const inputRef = useRef(null); useImperativeHandle(ref, () => inputRef.current); return <input type="text" name="child input" ref={inputRef} />; } const ChildInput = forwardRef(ChildInputComponent); function App() { const inputRef = useRef(null); useEffect(() => { inputRef.current.focus(); }, []); return ( <p> <ChildInput ref={inputRef} /> </p> ); }
通过这种方式,App 组件可以获得子组件的 input 的 DOM 节点。
大部分情况下,使用 useEffect 就可以帮我们处理组件的副作用,但是如果想要同步调用一些副作用,比如对 DOM 的操作,就需要使用 useLayoutEffect,useLayoutEffect 中的副作用会在 DOM 更新之后同步执行。
function App() { const [width, setWidth] = useState(0); useLayoutEffect(() => { const title = document.querySelector("#title"); const titleWidth = title.getBoundingClientRect().width; console.log("useLayoutEffect"); if (width !== titleWidth) { setWidth(titleWidth); } }); useEffect(() => { console.log("useEffect"); }); return ( <p> <h1 id="title">hello</h1> <h2>{width}</h2> </p> ); }
在上面的例子中,useLayoutEffect 会在 render,DOM 更新之后同步触发函数,会优于 useEffect 异步触发函数。
简单来说就是调用时机不同,useLayoutEffect
和原来componentDidMount
&componentDidUpdate
一致,在react完成DOM更新后马上同步调用的代码,会阻塞页面渲染。而useEffect
是会在整个页面渲染完才会调用的代码。
官方建议优先使用useEffect
However, we recommend starting with useEffect first and only trying useLayoutEffect if that causes a problem.
在实际使用时如果想避免页面抖动(在useEffect
里修改DOM很有可能出现)的话,可以把需要操作DOM的代码放在useLayoutEffect
里。关于使用useEffect
导致页面抖动。
不过useLayoutEffect
在服务端渲染时会出现一个warning,要消除的话得用useEffect
代替或者推迟渲染时机。
推荐学习:《react视频教程》
The above is the detailed content of There are several hooks in react. For more information, please follow other related articles on the PHP Chinese website!