create-react-class is a drop-in replacement for React.createClass, providing legacy API for creating React component classes. It maintains the original createClass interface including lifecycle methods, mixins support, and automatic method binding, serving as a compatibility layer for legacy React applications migrating from createClass syntax to newer React versions.
npm install create-react-classimport createReactClass from "create-react-class";For CommonJS:
const createReactClass = require("create-react-class");For browser globals (UMD):
<script src="https://unpkg.com/create-react-class/create-react-class.min.js"></script>
<script>
// Available as global createReactClass
</script>import React from "react";
import createReactClass from "create-react-class";
// Create a component class using the legacy API
const MyComponent = createReactClass({
getInitialState() {
return { count: 0 };
},
handleClick() {
this.setState({ count: this.state.count + 1 });
},
render() {
return React.createElement("div", null,
React.createElement("p", null, `Count: ${this.state.count}`),
React.createElement("button", { onClick: this.handleClick }, "Increment")
);
}
});
// Use the component
React.render(React.createElement(MyComponent), document.getElementById("root"));Creates React component classes using the legacy createClass API with automatic method binding and lifecycle support.
/**
* Creates a React component class from a specification object
* @param {ComponentSpec} spec - Component specification containing methods and configuration
* @returns {ComponentConstructor} React component class constructor
*/
function createReactClass(spec: ComponentSpec): ComponentConstructor;
interface ComponentSpec {
// Required method
render(): ReactElement;
// Lifecycle methods (optional)
getInitialState?(): object | null;
getDefaultProps?(): object;
getChildContext?(): object;
componentWillMount?(): void;
UNSAFE_componentWillMount?(): void;
componentDidMount?(): void;
componentWillReceiveProps?(nextProps: object, nextContext?: object): void;
UNSAFE_componentWillReceiveProps?(nextProps: object, nextContext?: object): void;
shouldComponentUpdate?(nextProps: object, nextState: object, nextContext?: object): boolean;
componentWillUpdate?(nextProps: object, nextState: object, nextContext?: object): void;
UNSAFE_componentWillUpdate?(nextProps: object, nextState: object, nextContext?: object): void;
componentDidUpdate?(prevProps: object, prevState: object, prevContext?: object): void;
componentWillUnmount?(): void;
// Configuration (optional)
displayName?: string;
mixins?: Mixin[];
statics?: { [key: string]: any };
propTypes?: { [key: string]: PropTypeValidator };
contextTypes?: { [key: string]: PropTypeValidator };
childContextTypes?: { [key: string]: PropTypeValidator };
autobind?: boolean;
// Static lifecycle methods (optional)
getDerivedStateFromProps?(props: object, state: object): object | null;
// Advanced (optional)
updateComponent?(): void;
// Custom methods (optional)
[methodName: string]: any;
}
interface ComponentConstructor {
new (props?: object, context?: object, updater?: object): ComponentInstance;
displayName?: string;
defaultProps?: object;
propTypes?: { [key: string]: PropTypeValidator };
contextTypes?: { [key: string]: PropTypeValidator };
childContextTypes?: { [key: string]: PropTypeValidator };
getDerivedStateFromProps?: (props: object, state: object) => object | null;
}
interface ComponentInstance {
props: object;
state: object | null;
context: object;
refs: { [key: string]: any };
// Methods available on instances
setState(partialState: object | ((prevState: object, props: object) => object), callback?: () => void): void;
forceUpdate(callback?: () => void): void;
// Legacy methods (deprecated)
isMounted(): boolean;
replaceState(newState: object, callback?: () => void): void;
}
interface Mixin {
// Any valid ComponentSpec properties
[key: string]: any;
}
type PropTypeValidator = (props: object, propName: string, componentName: string) => Error | null;The createClass system supports different method composition policies:
render, shouldComponentUpdate)getInitialState, getDefaultProps)All non-React methods defined in the spec are automatically bound to the component instance unless autobind: false is specified.
const Component = createReactClass({
handleClick() {
// 'this' is automatically bound to component instance
console.log(this.props.name);
},
render() {
return React.createElement("button", { onClick: this.handleClick }, "Click me");
}
});Components can include mixins to share functionality across multiple components.
const TimerMixin = {
componentDidMount() {
this.timer = setInterval(() => {
this.setState({ time: Date.now() });
}, 1000);
},
componentWillUnmount() {
clearInterval(this.timer);
}
};
const TimedComponent = createReactClass({
mixins: [TimerMixin],
getInitialState() {
return { time: Date.now() };
},
render() {
return React.createElement("div", null, new Date(this.state.time).toString());
}
});Define static methods and properties using the statics configuration.
const Component = createReactClass({
statics: {
customMethod() {
return "I'm a static method";
},
VERSION: "1.0.0"
},
render() {
return React.createElement("div", null, Component.customMethod());
}
});Full support for PropTypes validation and React context.
const Component = createReactClass({
propTypes: {
name: PropTypes.string.isRequired,
age: PropTypes.number
},
contextTypes: {
theme: PropTypes.string
},
childContextTypes: {
user: PropTypes.object
},
getChildContext() {
return {
user: { name: this.props.name, age: this.props.age }
};
},
render() {
const theme = this.context.theme || "default";
return React.createElement("div", { className: theme }, this.props.name);
}
});This package provides a seamless migration path from the deprecated React.createClass:
Before (React <16):
const Component = React.createClass({
render() {
return React.createElement("div", null, "Hello");
}
});After (React 16+):
import createReactClass from "create-react-class";
const Component = createReactClass({
render() {
return React.createElement("div", null, "Hello");
}
});The package includes comprehensive error checking and provides detailed error messages for:
render methodAll errors include component names and helpful debugging information when available.
process.env.NODE_ENVSupports all browsers that React supports, with automatic polyfills for older environments through the build process.