user99999
user99999

Reputation: 2024

Calling function with call in jsx

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?

Upvotes: 1

Views: 1013

Answers (1)

MariuszJasinski
MariuszJasinski

Reputation: 504

Change to, sir :

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

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

Upvotes: 2

Related Questions