0

I am trying to separate the SetIntervalMixin into a different file that the component class file. Maybe I am not fully understand how module.export works but... If I do like this:

module.exports = {
 componentWillMount: function() {
   this.intervals = [];
 },
 setInterval: function() {
   this.intervals.push(setInterval.apply(null, arguments));
 },
 componentWillUnmount: function() {
   this.intervals.map(clearInterval);
 }
};

inside a SetIntervalMixin.js, then it works fine using from the component:

var SetIntervalMixin = require('../util/mixins/SetIntervalMixin')

But if I write it like this:

var SetIntervalMixin = {

  componentWillMount: function() {
    this.intervals = [];
  },
  setInterval: function() {
    this.intervals.push(setInterval.apply(null, arguments));
  },
  componentWillUnmount: function() {
    this.intervals.map(clearInterval);
  }
};

module.export = SetIntervalMixin;

It doesn't work (undefined when trying to call setInterval()). I think something is missing after:

SetIntervalMixin = ...

Like when you define a component, you use:

var yourComponent = React.createClass(...

Is there is something similar like a React.createMixin(.. ? Or how would be the best way to do this.

Thanks.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
Ferran Negre
  • 3,712
  • 3
  • 34
  • 56

1 Answers1

4

Your code is right, you just have a typo in the second version (should be module.exports instead of module.export):

var SetIntervalMixin = {

  componentWillMount: function() {
    this.intervals = [];
  },
  setInterval: function() {
    this.intervals.push(setInterval.apply(null, arguments));
  },
  componentWillUnmount: function() {
    this.intervals.map(clearInterval);
  }
};

module.exports = SetIntervalMixin;
go-oleg
  • 19,272
  • 3
  • 43
  • 44
  • 1
    Wow! You're right. Damn, what a mistake! My bad of using IntelliJ... I want to install Atom in RedHat (with the awesome react snippets - highlighting plugin) but I haven't been lucky so far. Anyway, thank you! – Ferran Negre Aug 25 '14 at 14:33