0

In this code whenever i try to access the props of beforeShow() in show() function it shows undefined is there any way i can resolve this? and also i've been trying to animate the spans of title in show() and thats what i am trying to get in show(). this.elements.titleSpans i am trying to animate it. and also getting the error from GSAP like GSAP target not found or undefined. every time i try to log them in show() it shows undefined in the console. || I am a newbie i would like to have answered in this question i not understand a thing of the solution you mentioned in the private feedback. Please let me get help with this thing already its been weeks trying to solve the error :c.||

import GSAP from 'gsap';

import Page from '../classes/Page';

import { split } from '../utils/text';

export default class AboutPage extends Page {
  constructor() {
    super({
      element: '.about',
      elements: {
        title: '.about__title',
        description: '.about__description',
        content: '.about__content',
      },
    });
  }

  beforeShow() {
    super.beforeShow();

    this.elements.titleSpans = split({
      element: this.elements.title,
      expression: '<br>',
    });
  }

  show() {
    this.timelineIn = GSAP.timeline();
    this.timelineIn.fromTo(
      this.element,
      {
        autoAlpha: 0,
      },
      {
        autoAlpha: 1,
        duration: 0.4,
      }
    );

    if (this.elements.titleSpans === true) {
      this.timelineIn.fromTo(
        this.elements.titleSpans,
        {
          autoAlpha: 0,
          opacity: 0,
          y: '-50%',
          skewY: '-10deg',
        },
        {
          opacity: 1,
          autoAlpha: 1,
          duration: 0.4,
          y: '0%',
          skewY: '0deg',
          stagger: {
            amount: 0.1,
          },
        }
      );
    } else {
      console.log('There is no element like that');
    }

    return super.show(this.timelineIn);
  }

  hide() {
    this.timelineOut = GSAP.timeline();
    this.timelineOut.to(this.element, {
      autoAlpha: 0,
      duration: 0.4,
    });

    return super.hide(this.timelineOut);
  }
}

The file is being extended from Page.js

import Component from './Component';

export default class Page extends Component {
  constructor({ element, elements }) {
    super({
      autoMount: false,
      element,
      elements: {
        ...elements,
        images: 'img',
      },
    });

    this.components = [];
  }

  /**
   * Animations.
   */
  beforeShow() {
    if (this.elements.images) {
      if (!this.elements.images.length) {
        this.elements.images = [this.elements.images];
      }

      this.elements.images.forEach(image => {
        image.setAttribute('src', image.getAttribute('data-src'));
      });
    }
  }

  show(animation) {
    this.beforeShow();

    return new Promise(async resolve => {
      if (animation) {
        await animation;
      } else {
        console.warn(`Page doesn't have animation in set.`);
      }

      this.afterShow();

      resolve();
    });
  }

  afterShow() {}

  beforeHide() {}

  hide(animation) {
    this.beforeHide();

    return new Promise(async resolve => {
      if (animation) {
        await animation;
      } else {
        console.warn(`Page doesn't have animation out set.`);
      }

      this.afterHide();

      resolve();
    });
  }

  afterHide() {}

  /**
   * Events.
   */
  onMouseDown(event) {
    this.components.forEach(component => component.onMouseDown?.(event));
  }

  onMouseMove(event) {
    this.components.forEach(component => component.onMouseMove?.(event));
  }

  onMouseUp(event) {
    this.components.forEach(component => component.onMouseUp?.(event));
  }

  onResize(event) {
    this.components.forEach(component => component.onResize?.(event));
  }

  /**
   * Loop.
   */
  update() {
    this.components.forEach(component => component.update?.());
  }
}
  • Not much changed in the code from your previous question: [How I can able to access the Function properties to other function?](https://stackoverflow.com/q/71372544/4642212). Have you tried applying the advice from the linked post? – Sebastian Simon Mar 14 '22 at 21:30
  • What do you mean by "*the props of `beforeShow()`*"? What exactly is undefined, and where? How are you calling all this? – Bergi Mar 14 '22 at 22:10
  • Btw, [never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Mar 14 '22 at 22:13
  • by props of `beforeShow()` i mean the element in `beforeShow()` i am trying to access it in `show()` by doing `console.log(this.elements.titleSpans` it shows `undefined` @Bergi – Raj Shukla Mar 15 '22 at 05:31
  • I not able to understand it so :c @SebastianSimon but i really appreciate your efforts and time sir – Raj Shukla Mar 15 '22 at 05:32
  • @RajShukla Well that's because `beforeShow()` is called from `super.show(this.timelineIn)`, *after* you tried to access `this.elements.titleSpans` – Bergi Mar 15 '22 at 09:22
  • @Bergi so what should i do here to resolve the issue i am having :) ? – Raj Shukla Mar 16 '22 at 19:12
  • I guess, just mvoe the `this.elements.titleSpans = …` inside your `show` method? Maybe even get rid of `beforeShow` completely, not quite sure what you're using it for. – Bergi Mar 16 '22 at 19:21
  • @Bergi yeah i did that before which worked but thats now what i wanted to have here if i can able to achieve what i wanted to achieve here would be very good as you can see the Page.js code where you can see that They should be already available in the show(), because the beforeShow happens before this. – Raj Shukla Mar 16 '22 at 20:11
  • No, `beforeShow` does not happens before `show`. It happens *within* `show`. Which is maybe what you should change. – Bergi Mar 16 '22 at 20:56
  • @Bergi yeah thats y it shows undefined it was expected to happened before it is there anything can be done where we not have to move the things of beforeShow to any function and make it work? so what should i change here – Raj Shukla Mar 16 '22 at 21:01
  • Don't call `beforeShow()` from `show`. Call it *before* `show()`, everywhere where you are calling `show()`. – Bergi Mar 16 '22 at 21:04
  • this is the link of that project on codesandbox https://codesandbox.io/s/gracious-ramanujan-tmt0ld?file=/README.md – Raj Shukla Mar 16 '22 at 22:11

0 Answers0