Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Length is Undefined #6

Open
joebentaylor1995 opened this issue Jul 7, 2020 · 3 comments
Open

Length is Undefined #6

joebentaylor1995 opened this issue Jul 7, 2020 · 3 comments

Comments

@joebentaylor1995
Copy link

Getting this error:

index.js:192 Uncaught TypeError: Cannot read property 'length' of undefined
    at MagnetMouse.magnetElement (index.js:192)
    at index.js:251
    at index.js:57

my code: (React)

// ------
import React, { useEffect } from 'react'
import MagnetMouse from 'magnet-mouse';

// Styles
// ------
import {
    Jacket,
    InnerCircle
} from './styles';

// Component
// ------
const Cursor = () => {
    useEffect(() => {
        let mm = new MagnetMouse({
            follow: {
                element: '.cursor',
                class: 'follow-mouse-active' /* Add class to element that follows the mouse when enter in the magnet effect */
            },
        });

        mm.init();
    }, [])

    return (
        <Jacket className="cursor">
            <InnerCircle />
        </Jacket>
    );
}

export default React.memo(Cursor);
@jamestsang
Copy link

It hasn't assigns posElement by getElementsPosition()
You may addposElement = _this.getElementsPosition(); to line 268
or trigger window resize by your self.

@Skamnoel
Copy link

just faced the same Error ( also using this lib in my React component )
As @jamestsang said, triggering resize manually fixes it.
Just do it immediately after you call mm.init() with this: window.dispatchEvent(new Event('resize'))

@valentinilas
Copy link

I think it's because it tries to get the element position on DOMContentLoaded and your script runs after that event fires, so posElement remains undefined

      // Calculate position of element when page load
      document.addEventListener('DOMContentLoaded', () => {
        posElement = this.getElementsPosition();
      });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants