question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: Cannot access 'variant' before initialization

See original GitHub issue

I feel myself a little bit stupid because this should be simple, but I’m not fixing it…

If I try to access to variant in an Complete method, I’ve an error.

 const { variant } = useMotion(ref, {
    initial: {
      y: 0,
      scale: maxZoomTimes * 0.8,
      opacity: 0
    },
    enter: {
      y: 0,
      scale: maxZoomTimes,
      opacity: 1,
      transition: {
        delay: extraDelay,
        duration: 800,
        onComplete() {
          variant.value = 'idle';
        }
      }
    },
    idle: {
      y: 10,
      scale: maxZoomTimes,
      opacity: 1,
      transition: {
        duration: 2000,
        repeat: Infinity,
        ease: 'easeInOut',
        repeatType: 'mirror'
      }
    }
  });

Error: Uncaught (in promise) ReferenceError: Cannot access 'variant' before initialization at Object.onComplete

The code works pefect but I have the error on my console.

I think that this is happening because in the object I’ve external variables like maxZoomTimes and JavaScript evaluates the object before variant exist, if instead of using variables you have a raw object everything works fine.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
CKGraficocommented, Aug 2, 2022

Meanwhile we wait for an official answer I have a kind of workaround… not my best code but I’ll work.

First of all I’ve created some custom types:

import { MotionInstance, MotionProperties, MotionVariants, Transition } from '@vueuse/motion';

type Variant = {
  transition?: Transition;
} & MotionProperties;

export interface CustomMotionVariants extends MotionVariants {
  idle?: Variant;
}

and then…

 const instance = useMotion<CustomMotionVariants>(ref, {
    initial: {
      transform: 'translateY(0vh)',
      opacity: 0
    },
    enter: {
      transform: 'translateY(-25vh)',
      opacity: 1,
      transition: {
        duration: 800,
        onComplete() {
          instance.variant.value = 'idle';
        }
      }
    },
    idle: {
      transform: 'translateY(-24vh)',
      opacity: 1,
      transition: {
        duration: 2000,
        repeat: Infinity,
        ease: 'easeInOut',
        repeatType: 'mirror'
      }
    }
  });
0reactions
CKGraficocommented, Sep 14, 2022

Is not with typescript support, is it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: Cannot access 'steps' before initialization
The error is telling you that the variable steps is initialized on line 7, but you're using it on line 6 to set...
Read more >
ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >
HMR error: Cannot access '...' before initialization · Issue #3033
Describe the bug The error happens when I try to edit component, that Wrap in connect function (redux) Is inside dependency loop There...
Read more >
cannot access before initialization webpack - You.com | The AI ...
This might be caused by a cyclic dependencies (i.e. moduleA imports module B and vice versa at the same time). Take a deeper...
Read more >
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found