0

I am having a very frustrating issue. I set my variable in constructor like this:

export class ChannelTransition{
    constructor(props) {
        this.transitionHandler=props;
    }

    channelTransitionSetup(channelsList) {
        console.log(this.transitionHandler.handleHeaderTransition);            
    }
}

And now when I try to access that variable set on constructor I get undefined. I know it has sth to do with scope but then how can I access my variable in constructor?

5
  • 2
    Make sure your transitionHandler has a property called handleHeaderTransition? Otherwise this is correct. Commented Sep 28, 2017 at 13:11
  • 1
    And where and how channelTransitionSetup is called? Commented Sep 28, 2017 at 13:11
  • Does props has any property named handleHeaderTransition? Did you debugged the code? What values did you get in the constructor for transitionHandler after setting it? what values are you getting inside channelTransitionSetup for transitionHandler before calling this.transitionHandler.handleHeaderTransition? Commented Sep 28, 2017 at 13:14
  • First I make an object of the class and then I call channelTransitionSetup. So I am sure I am calling it properly. Even when I use console.log in constructor I am able to see the right result Commented Sep 28, 2017 at 13:14
  • @palaѕн Yes it has and even I am able to see it in constructor but in the function maybe because of this scope it returns undefined Commented Sep 28, 2017 at 13:15

1 Answer 1

1

This works out for me:

    class ChannelTransition{
        constructor(props) {
            this.transitionHandler=props;
        }
        channelTransitionSetup(channelsList) {
            console.log(this.transitionHandler.handleHeaderTransition);

        }

    }


    let test = new ChannelTransition({handleHeaderTransition:'Ok!'});

    test.channelTransitionSetup();
/* This print 'Ok!' in console */

So I do not know what is your problem. Can you explain how do you use it?

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answering: here is how I am calling it: let channelTransition = new ChannelTransition(this.props.handleHeaderTransition); channelTransition.changeChannelOnScroll(channelNames); and then in changeChannelOnScroll I have this.channelTransitionSetup(channelsList);
Also I just simplified the above code and there is a reason that I do not use the channelTransitionSetup directly
I think I found the issue . Your example helped me a lot. So I was going one too many level down in the object hierarchy. Instead if I call it this way it works: this.transitionHandler() instead of this.transitionHandler.handleHeaderTransition(). Thanks for the help
It's a pleasure. If you get any other problem just ask over here :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.