1

I'm trying to call a function from outside a class, and I want this function to have access to the same this variable (which means to props, refs, etc). I've been trying:

const something = (() => {
    console.log(this); // undefined
});

class FooBar extends React.Component {

    render () {
        return (
            <div>
                {something.call(this)}
            </div>
        );
    };

};

However, using Function.prototype.call, this is still undefined. Same thing happens with apply, and bind doesn't execute a function immediately. What's wrong?

1
  • Could you try this : const something = function() { console.log(this); } ? Commented Dec 29, 2016 at 10:47

1 Answer 1

2

Change to, sir :

const something = function() {  
    console.log(this); // FooBar
};

Pen: http://codepen.io/enfer/pen/qqejaV

2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.