March 27, 2022

useMousePosition()

import {useState, useEffect} from 'react';

const useMousePosition = () => {  
   const [    
     mousePosition,    
     setMousePosition  
   ] = useState({ x: null, y: null });  
   
   useEffect(() => {    
     const updateMousePosition = env => {      
        setMousePosition({ x: env.clientX, y: env.clientY });    
     };    
     
     window.addEventListener('mousemove', updateMousePosition);   
      
     return () => {      
        window.removeEventListener('mousemove', updateMousePosition);    
     };  
   }, []);  
   
   return mousePosition;
};

export default useMousePosition;

Bu custom hook orqali sichqonchaning y va x o'qi bo'yicha joylashuv qiymatlarini olishimiz mumkin.