1. What are custom hooks
Simply put, using custom hooks can extract certain component logic into reusable functions. A custom hook is a Javascript function that calls other hooks starting from use . 2. When not using custom hooksExample 1: When our entire page needs to obtain the coordinates of the user's mouse movement, without using the hook code, we can write const [position, setPosition] = useState({ x: 0, y: 0 }) useEffect(() => { const move = (e) => { setPosition({ x: ex, y: ey }) } document.addEventListener('mousemove', move) return () => { document.removeEventListener('mousemove', move) } }, []) return ( <div> x:{position.x} y:{position.y} </div> ) Example 2: When we have an image on our page that follows the mouse, we can also write it like this without using the hook code: const [position, setPosition] = useState({ x: 0, y: 0 }) useEffect(() => { const move = (e) => { setPosition({ x: ex, y: ey }) } document.addEventListener('mousemove', move) return () => { document.removeEventListener('mousemove', move) } }, []) return ( <div> <img src={img} style={{ position: 'absolute', top: position.y, left: position.x, }} alt="" /> </div> ) Obviously, the above two examples have different presentation effects, but the logic codes used are mostly the same. We can use hooks to reuse the logic codes. 3. Use custom hookWe extract the reusable logic code from the above two examples and create a new file called useMousePosition import { useState, useEffect } from 'react' export default function useMousePosition() { const [position, setPosition] = useState({ x: 0, y: 0 }) useEffect(() => { const move = (e) => { setPosition({ x: ex, y: ey }) } document.addEventListener('mousemove', move) return () => { document.removeEventListener('mousemove', move) } }, []) return position } We extracted this functionality in the useMousePosition function. Now we can import it wherever we want to use it! Finally, use it like a normal function const position = useMousePosition() return ( <div> x:{position.x} y:{position.y} </div> ) Obviously, the amount of code is reduced This is the end of this article about creating custom hooks in react. For more relevant react custom hooks content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Example of using docker compose to build a consul cluster environment
>>: The use of textarea in html and common problems and case analysis
1. JS asynchronous execution principle We know th...
Using depends_on to sort containers does not perf...
pthread_create function Function Introduction pth...
The downloaded version is the Zip decompression v...
What to do if you forget your Linux/Mac MySQL pas...
Nginx uses regular expressions to automatically m...
Features of SSHFS: Based on FUSE (the best usersp...
Table of contents 1. Install JDK Manual Installat...
The questions encountered in Baidu interviews nee...
Application of HTML and CSS in Flash: I accidental...
MySQL5.6 How to create SSL files Official documen...
This article shares the specific code of jQuery t...
Table of contents js deep copy Data storage metho...
MySQL download and installation (version 8.0.20) ...
This article shares the specific code of Vue recu...