[SOLVED] ES6 With mixins: [ReactMeteorData]

How to use mixins: [ReactMeteorData] with ES6?

import React from 'react';

export default class App extends React.Component {
  
  mixins: [ReactMeteorData], // Unexpected token

  render() {

  ...

}

How to use this mixin? I know it works with ES5 but not for ES6+

Edited:

Will mixins be supported in the new js? This makes me now wonder if true.

1 Like

You should use React.createClass(), not ES6 classes, if you want to use ReactMeteorData (and trust me, you do). There’s no compelling reason to make your React components via classes.

3 Likes

I was just thinking the same. I thought that’s where the meteor and js eco was heading.

You could just grab the code for the ReactMeteorData mixin and throw it into your project and hack it in. There are other hacks out there, but personally I’d avoid the headache.

I’ll have a fiddle but I hate headaches too :wink:

I use it like that:

import ReactMixin from ‘react-mixin’;

@ReactMixin.decorate(ReactMeteorData)
export default class App extends Component {

getMeteorData() { }

}

@thsering

While processing files with ecmascript (for target web.browser):
client/components/App/App.jsx:4: /client/components/App/App.jsx: Unexpected token (4:0)

ReactMeteorData is installed with meteor add …

Line 4 is @ReactMixin.decorate(ReactMeteorData)

Do you use NPM packages? Think that React-Mixin is missing…

npm install react-mixin --save ? Yes

This one: https://www.npmjs.com/package/react-mixin ?

It’s the right package, yes. How you include it depends on your project really.
With MeteorHacks:NPM for instance. Unless you’re already using Meteor 1.3

:sweat_smile: Yeaaaaa 1.3

Cool. Haven’t touched it yet but I guess you just use npm install and are ready to go. It’s just a guess though :grinning:

Thanks. I’ll blame 1.3 but not a huge deal.

I’ll just use this method for now but that means I’d have to do this for every route

Haha…no don’t blame Meteor. It’s my fault - noob and all that. So, decorators won’t work in ES6 (it’s ES7). You’d have to use the mixin differently. Try:

reactMixin.class(App, ReactMeteorData)

like so:

import React from 'react';
import reactMixin from 'react-mixin';

reactMixin.class(App, ReactMeteorData)
export default class App extends React.Component {

...

I’d think so…gonna test it myself now :slight_smile:

Uncaught TypeError: _reactMixin2.default.class is not a function

import React from ‘react’;
import reactMixin from ‘react-mixin’;

reactMixin.class(App, ReactMeteorData)
export default class App extends React.Component {

}

Right. Saw that too. So put that mixin below your component and change the line to:

reactMixin(App.prototype, ReactMeteorData);

2 Likes

Sweet! Thanks! Works.

It always does in the end :slight_smile:

1 Like