How do I implement "normal" ES5 prototypal inheritance in React?

I am under the impression that ES6 classes are basically syntactic sugar around the ES5 object system. Since I'm trying to run React without a transpiler, I figured I can only use the old syntax to define "classes" objects that "inherit" from React.Component.

    var Board = function(props, state) {
        var instance = {};

        instance.props = props;
        instance.context = state;

        return(instance);           
    };

    Board.prototype = Object.create(React.Component.prototype);

    Board.prototype.render = function() {
        return(
            // ...stuff
        )               
    };

      

But it doesn't work!

react.js:20478 Warning: Board(...): No `render` method found on the returned component instance: you may have forgotten to define `render`
react.js:6690 Uncaught TypeError: inst.render is not a function(…)

      

I found alternatives in this context and the following works:

    var Board = function(props, state) {
        var instance = Object.create(React.Component.prototype);

        instance.props = props;
        instance.context = state;

        instance.prototype.render = function() {
            return(
                // ...stuff
            )               
        };

        return(instance);           
    };

      

I also figured out that I can use a helper React.createClass

.

But I would still like to understand why React won't handle classes defined in this general way. It seems to me that ES6 classes are used prior to use. I see no reason why ES5 class classes would also not be driven by similar results.

+2


source to share


1 answer


Why isn't "normal" ES5 prototypal inheritance supported in React?

This use React.createClass

is probably your best option though . It's just that the code in your question doesn't do standard ES5-like inheritance tasks. In particular:

  • You are returning an instance of a simple object, not an instance Board

    , and therefore is Board.prototype

    not used by the object. Typically, a constructor function should return nothing and should use the invoked object new

    that it receives as this

    .
  • You are not giving the React.Component

    opportunity to initialize the instance.
  • You don't install constructor

    on Board.prototype

    (although I don't know if React cares which it doesn't).


It works if you set it up in the usual way. Here's an ES5 example without React.createClass

, see comments:

// The component
function Foo(props) {
    // Note the chained superclass call
    React.Component.call(this, props);
}

// Set up the prototype
Foo.prototype = Object.create(React.Component.prototype);
Foo.prototype.constructor = Foo; // Note

// Add a render method
Foo.prototype.render = function() {
    return React.createElement("div", null, this.props.text);
};

// Use it
ReactDOM.render(
    React.createElement(Foo, {
        text: "Hi there, the date/time is " + new Date()
    }),
    document.getElementById("react")
);
      

<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
      

Run codeHide result


+5


source







All Articles