Extending React.js components Extending React.js components reactjs reactjs

Extending React.js components


To get something that resembles inheritance (actually composition as pointed out in comments), you can make an Airplane in your example wrap itself in a Vehicle. If you want to expose methods on Vehicle in the Airplane component, you can use a ref and connect them one-by-one. This is not exactly inheritance (it's actually composition), particularly because the this.refs.vehicle will not be accessible until after the component has been mounted.

var Vehicle = React.createClass({    ...});var Airplane = React.createClass({    methodA: function() {      if (this.refs != null) return this.refs.vehicle.methodA();    },    ...    render: function() {        return (            <Vehicle ref="vehicle">                <h1>J/K I'm an airplane</h1>            </Vehicle>        );    }});

Also it's worth mention that in the React official documentation they prefer composition over inheritance:

So What About Inheritance? At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.

Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.

If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.

Another thing worth mention that using ES2015/ES6+ you can also spread the object props from Airplane component to the Vehicle component

render: function() {    return (        <Vehicle {...this.props}>            <h1>J/K I'm an airplane</h1>        </Vehicle>    );}


If you use webpack+babel+react NPM package set up in your project, then in ES6 OOP implementation it should be as simple as this:

import React, { Component } from 'react';import ReactDOM from 'react-dom';class BaseComponentClass extends Component {  componentDidMount() {}   render() { return (<div>Base Component</div>) }}class InheritedComponentClass extends BaseComponentClass {  // componentDidMount is inherited  render() { return (<div>Inherited Component</div>) } // render is overridden }ReactDOM.render(<InheritedComponentClass></InheritedComponentClass>,      document.getElementById('InheritedComponentClassHolder'));

Note: Here is a simple starter kit with such set up: https://github.com/alicoding/react-webpack-babel


By leveraging ReactOO, you can easily use inheritance (Disclaimer: I'm the author of ReactOO):

(function () {    // Class definitions. ReactOO requires class definition first just like you did in an OO way.    window.Vehicle = window.ReactOO.ReactBase.extend({        getReactDisplayName: function () {            return 'Vehicle';        },        onReactRender: function (reactInstance) {            var self = this;            var text = self.methodC();            return React.createElement('div', {}, text);        },        methodA: function () {            console.log('Vehicle method A');        },        methodB: function () {            console.log('Vehicle method B');        },        methodC: function () {            return 'Vehicle method C';        }    });    window.Airplane = window.Vehicle.extend({        getReactDisplayName: function () {            return 'Airplane';        },        methodC: function () {            // Call this._super() to execute parent method.            //this._super();            return 'Airplane method C';        }    });    var vehicle = new window.Vehicle();    vehicle.render({}, '#vehicle');    var airPlane = new window.Airplane();    airPlane.render({}, '#airPlane');})();
<!DOCTYPE html><html><head>    <meta charset="utf-8" />    <title>ReactOO Vehicle-Aireplane Example</title>    <link rel="stylesheet" href="css/base.css" />    <script src="scripts/react.js"></script>    <script src="scripts/react-dom.js"></script>    <script src="../src/reactoo.js"></script></head><body>    <div id="vehicle">    </div>    <div id="airPlane">    </div>    <script src="scripts/vehicleAirplane.js">    </script></body></html>