Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use getInitialState in a React component

Tags:

reactjs

I have a React component that toggles a className when the component is clicked

var Foo = React.createClass({
  getInitialState: function() {
    return {className: ''}
  },

  render: function(){
    var className = 'bar ' + this.state.className
    return React.createElement('div', {className: className, onClick: this.onClick})
  },

  onClick: function() {
    this.setState({className: 'baz'})
  }
});

It works fine, but when I am rendering the app server side, I get the following error

Warning: getInitialState was defined on a component, a plain JavaScript class.
This is only supported for classes created using React.createClass.
Did you mean to define a state property instead?

My build step is setup like so

var Foo = require('./Foo');
var factory = React.createFactory(Foo);
module.exports = React.renderToString(factory({}));

Why is what I am doing wrong, and how should it be done?

like image 450
Patrick Avatar asked Mar 17 '15 02:03

Patrick


1 Answers

I am not sure if this helps, but while using fluxible, this is the syntax i used with JSX as part of require component

var app = new Fluxible({
  component: React.createFactory(require('./Components/startup.react.jsx'))
});
like image 80
Rajat banerjee Avatar answered Oct 20 '22 00:10

Rajat banerjee