Skip to content

this is undefined for React components #37

Closed

Description

@thom4parisot

I have a jsx file which is roughly the following:

let React = require('react');

module.exports = React.createClass({
  getInitialState: () => { username: 'John Doe' },

  render: () => {
    return (<div>{this.state.username}</div>);
  }
});

When I browserify my code, the outcome is:

var React = require('react');

module.exports = React.createClass({
  getInitialState: function() {
    return { username: 'John Doe' };
  },

  render: function() {
    return React.createElement('div', null, undefined.state.username);
  }
});

Notice the undefined.state.username bit. Any idea where it comes from?

Thanks :-)

Activity

  1. sebmck commented on Feb 10, 2015

    @sebmck

    See babel/babel#733. Arrow functions aren't equivalent to normal functions, whenever you use this inside them it refers to the outer this.

  2. thom4parisot commented on Feb 10, 2015

    @thom4parisot
    Author

    OK I see thanks! I suppose this would have worked if I used the Component extends React.Component and the new method definition (aka render(){ ... }. Thanks :-)

  3. sebmck commented on Feb 10, 2015

    @sebmck

    You can also use the method shorthand syntax inside object literals like so:

    let React = require('react');
    
    module.exports = React.createClass({
      getInitialState() { return username: 'John Doe'; },
    
      render() {
        return (<div>{this.state.username}</div>);
      }
    });
    
  4. thom4parisot commented on Feb 10, 2015

    @thom4parisot
    Author

    👍 ah that's great thanks! I did not know it worked for non-class declarations. Thanks :-)

  5. sebmck commented on Feb 10, 2015

    @sebmck

    No problem! Happy to answer any questions, feel free to check out our gitter room too.

  6. IngwiePhoenix commented on Nov 26, 2015

    @IngwiePhoenix

    Sorry for butting in all of a sudden :)

    I just began using Babel alltogether and realized, that sometimes this:

    (function(){
        // ...
    }).call(this);

    Would become this:

    (function(){
        // ...
    }).call(undefined);

    instead. Namely, the undefined-treatment happens against this line: https://github.com/musictheory/oj/blob/master/src/runtime.js#L375

    What is the best way to get rid of that behaviour, altogether?

  7. zertosh commented on Nov 27, 2015

    @zertosh
    Member

    @IngwiePhoenix That's being done by babel-plugin-transform-es2015-modules-commonjs. It's part of babel-preset-es2015. You can turn off transforming the top level this to undefined by passing it {allowTopLevelThis: true}. Like this:

    plugins: [
      [require('babel-plugin-transform-es2015-modules-commonjs'), {allowTopLevelThis: true}]
    ]

    That of course means that you can't use a preset, you'll have to specify the transforms individually so you can pass in that option.

  8. IngwiePhoenix commented on Nov 27, 2015

    @IngwiePhoenix

    Really? How come this won't work with a preset?

    2015-11-27 5:30 GMT+01:00 Andres Suarez [email protected]:

    @IngwiePhoenix https://github.com/IngwiePhoenix That's being done by
    babel-plugin-transform-es2015-modules-commonjs. It's part of
    babel-preset-es2015 http://babeljs.io/docs/plugins/preset-es2015/. You
    can turn off transforming the top level this to undefined by passing it {allowTopLevelThis:
    true}
    https://github.com/babel/babel/blob/v6.2.2/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js#L127.
    Like this:

    plugins: [
    [require('babel-plugin-transform-es2015-modules-commonjs'), {allowTopLevelThis: true}]
    ]

    That of course means that you can't use a preset, you'll have to specify
    the transforms individually so you can pass in that option.

    —
    Reply to this email directly or view it on GitHub
    #37 (comment).

  9. zertosh commented on Nov 27, 2015

    @zertosh
    Member

    Presets don't take options.

  10. brendensuarez commented on Apr 8, 2016

    @brendensuarez

    So the workaround to get this scoped to the parent scope is the old var that = this? That's a shame. Also a shame you can't pass in options with the Babel preset :(

  11. loganfsmyth commented on Apr 8, 2016

    @loganfsmyth
    Member

    @brendensoares It's not clear what you're asking. I'd recommend dropping by our documented support channels or #javascript on Freenode if you have specific ES6 questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions