React.createClass

Removed in 15.5
JavaScript classes were introduced in ES 2015.
React originally included its own class syntax that allowed you to generate class components.

var myClass = React.createClass ({ 
}

However after the success and quick adoption of ES 2015 this class syntax was removed in React version 15.5.
This syntax was moved to a separate module called [[create-react-class]]


Defining Classes

// With ES 2015 
import React from 'react'
class myClass extends React.Component {
   render() {
      return ( <h1>Title</h1> )
   }
}

// With ES 2009
var React = require('react')
var createReactClass = require('create-react-class')
var myClass = createReactClass( {
   render : function() {
      return React.createElement("h1", null, "Title");
   }
} )

Declaring Default Props

// With ES 2015 
import React from 'react'
class myClass extends React.Component {
}
myClass.defaultProps = {
   property1 : 'some text'
}

// With ES 2009
var React = require('react')
var createReactClass = require('create-react-class')
var myClass = createReactClass( {
   getDefaultProps : function() {
      return {
         property1 : 'some text'
      }
   }
} )

Setting the Initial State

// With ES 2015 
import React from 'react'
class myClass extends React.Component {
   constructor(props) {
      super(props);
      this.state = {
         state1 : this.props.property1
      };
   }
}

// With ES 2009
var React = require('react')
var createReactClass = require('create-react-class')
var myClass = createReactClass( {
   getInitialState : function() {
      return {
         state1 : this.props.property1
      }
   }
})

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext