motion icon indicating copy to clipboard operation
motion copied to clipboard

ReferenceError: Cannot access 'variant' before initialization

Open CKGrafico opened this issue 3 years ago โ€ข 6 comments

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.

CKGrafico avatar Jul 29 '22 13:07 CKGrafico

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'
      }
    }
  });

CKGrafico avatar Aug 02 '22 09:08 CKGrafico

fuzzy me , delay will work....

const { variant } = useMotion(waitBorder, {
  initial: { x: -50, opacity: 0 },
  enter: {
    y: 0,
    x: 0,
    opacity: 0.8,
    transition: {
      type: 'keyframes',
      ease: 'easeInOut',
      duration: 2000,
      onComplete: () => {
        setTimeout(() => { variant.value = 'levitate' }, 20)
      },
    },
  },
  levitate: {
    opacity: 0.2,
    transition: {
      duration: 2000,
      repeat: Infinity,
      ease: 'easeInOut',
      repeatType: 'mirror',
    },
  },
})


Leizhenpeng avatar Aug 12 '22 12:08 Leizhenpeng

Hello,

Could you please provide a reproduction to the issue?

Here is a link to a sandbox to help it: https://stackblitz.com/edit/vue-zj2zpq?file=src%2FApp.vue

Sorry for the late answer, I will try to investigate myself as well soon!

Tahul avatar Sep 11 '22 02:09 Tahul

No problem for the late reply @Tahul we understand :) I'm also busy these days but if you provide a stackblitz sandbox with motion and typescript I can copy paste the same code

CKGrafico avatar Sep 13 '22 05:09 CKGrafico

The link is here: stackblitz.com/edit/vue-zj2zpq?file=src%2FApp.vue ๐Ÿ˜„

Tahul avatar Sep 14 '22 00:09 Tahul

Is not with typescript support, is it?

CKGrafico avatar Sep 14 '22 09:09 CKGrafico

Would you be able to provide a reproduction? ๐Ÿ™

More info

Why do I need to provide a reproduction?

Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making.

What will happen?

If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritize it based on its severity and how many people we think it might affect.

If needs reproduction labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it.

How can I create a reproduction?

A link to a stackblitz project or public GitHub repository would be perfect. ๐Ÿ‘Œ

Please ensure that the reproduction is as minimal as possible.

You might also find these other articles interesting and/or helpful:

github-actions[bot] avatar Mar 21 '24 14:03 github-actions[bot]

This issue was closed because it was open for 7 days without a reproduction.

github-actions[bot] avatar Mar 29 '24 02:03 github-actions[bot]