React Basics~useEffect/ Pointer Animation~

・src/components/Pointer.tsx

import { useEffect, useState } from “react”;

const Pointer = () => {
const [position, setPosition] = useState({ x: 0, y: 0 });

function handleMove(e) {
setPosition({ x: e.clientX, y: e.clientY });
}

useE…


This content originally appeared on DEV Community and was authored by Ogasawara Kakeru

・src/components/Pointer.tsx

import { useEffect, useState } from "react";

const Pointer = () => {
  const [position, setPosition] = useState({ x: 0, y: 0 });

  function handleMove(e) {
    setPosition({ x: e.clientX, y: e.clientY });
  }

  useEffect(() => {
    window.addEventListener("pointermove", handleMove);
  }, []);

  return (
    <div
      style={{
        position: "absolute",
        backgroundColor: "blue",
        borderRadius: "50%",
        opacity: 0.6,
        pointerEvents: "none",
        transform: `translate(${position.x}px, ${position.y}px)`,
        left: -20,
        top: -20,
        width: 50,
        height: 50,
      }}
    ></div>
  );
};

export default Pointer;

・This component dispalys a pointer follows the mouse pointermoving.

・The function of the window object, in this case it is the addEventListener, is one of the side effects.

 useEffect(() => {
    window.addEventListener("pointermove", handleMove);
  }, []);

・The side effects be used in useEffect as you can see in this snippet.

・src/App.tsx

import "./App.css";
import Pointer from "./lessons/Lesson2/Lesson2_1/Pointer";

function App() {
  return (
    <div className="flex items-center max-w-4xl mx-auto py-8 text-2xl">
      <Pointer />
    </div>
  );
}

export default App;

・This component displays the Pointer componnet.

Image description


This content originally appeared on DEV Community and was authored by Ogasawara Kakeru


Print Share Comment Cite Upload Translate Updates
APA

Ogasawara Kakeru | Sciencx (2024-09-27T01:47:31+00:00) React Basics~useEffect/ Pointer Animation~. Retrieved from https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/

MLA
" » React Basics~useEffect/ Pointer Animation~." Ogasawara Kakeru | Sciencx - Friday September 27, 2024, https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/
HARVARD
Ogasawara Kakeru | Sciencx Friday September 27, 2024 » React Basics~useEffect/ Pointer Animation~., viewed ,<https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/>
VANCOUVER
Ogasawara Kakeru | Sciencx - » React Basics~useEffect/ Pointer Animation~. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/
CHICAGO
" » React Basics~useEffect/ Pointer Animation~." Ogasawara Kakeru | Sciencx - Accessed . https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/
IEEE
" » React Basics~useEffect/ Pointer Animation~." Ogasawara Kakeru | Sciencx [Online]. Available: https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/. [Accessed: ]
rf:citation
» React Basics~useEffect/ Pointer Animation~ | Ogasawara Kakeru | Sciencx | https://www.scien.cx/2024/09/27/react-basicsuseeffect-pointer-animation/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.