Legacy React Component Creation API

15.7.0 · deprecated · verified Sun Apr 19

The `create-react-class` package provides a way to define React components using a plain JavaScript object, mirroring the `React.createClass` API that was prevalent before the widespread adoption of ES6 classes and functional components with Hooks. Currently at version 15.7.0, this library serves as a drop-in replacement for the original `React.createClass`, which was extracted from the core `react` package into a standalone library. Its primary purpose is to support legacy codebases that have not yet migrated to modern React paradigms. The package itself is largely in a maintenance-only state, receiving no significant feature updates, as React development has shifted towards newer component patterns. Key differentiators include automatic `this` binding and support for `mixins`, features not present in ES6 class components or functional components.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating and rendering a simple stateful React component using the legacy `createReactClass` API, highlighting its object-based definition and automatic `this` binding.

import createReactClass from 'create-react-class';
import React from 'react';
import ReactDOM from 'react-dom';

const MyLegacyComponent = createReactClass({
  displayName: 'MyLegacyComponent',
  getInitialState: function() {
    return { count: 0 };
  },
  handleClick: function() {
    // `this` is automatically bound in createClass components
    this.setState({ count: this.state.count + 1 });
  },
  render: function() {
    return React.createElement(
      'div',
      null,
      React.createElement('h1', null, `Count: ${this.state.count}`),
      React.createElement('button', { onClick: this.handleClick }, 'Increment Count')
    );
  }
});

// For demonstration, render to a hypothetical root element
// In a real app, ensure 'root' element exists in your HTML
const rootElement = document.getElementById('root');
if (rootElement) {
  ReactDOM.render(
    React.createElement(MyLegacyComponent, null),
    rootElement
  );
} else {
  console.warn("Root element 'root' not found. Component not rendered.");
}

view raw JSON →