stephenscaff / react-animated-cursor

An animated custom cursor React component.
ISC License
273 stars 28 forks source link

Element Style Cleanup Incomplete Causes Invisible Cursor #67

Open MarkintoshZ opened 2 months ago

MarkintoshZ commented 2 months ago

Description

If AnimatedCursor is rendered conditionally, the cursor will stay invisible even when AnimatedCursor is removed from the render tree.

To Produce

import { useState } from "react";
import AnimatedCursor from "react-animated-cursor";

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

  return (
    <>
      <button onClick={() => setCount((count) => count + 1)}>
        count is {count}
      </button>
      {count <= 1 && <AnimatedCursor />}
    </>
  );
}

export default App;

Potential Fix

These lines for removing event listeners seems suspicious. Although they might not directly relate to this issue, they seem wrong. The correct way is to create a named function for the event handler and pass that to the removeEventListener function. Otherwise, the added listeners are separate object than the listeners that are specified to remove.