相关疑难解决方法(0)

如何在React中实现"正常"的ES5原型继承?

我的印象是ES6类基本上是围绕ES5对象系统的语法糖.当我试图在没有转换器的情况下运行React时,我想我可以使用旧语法来定义从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
        )               
    };
Run Code Online (Sandbox Code Playgroud)

但这不起作用!

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(…)
Run Code Online (Sandbox Code Playgroud)

在这个要点中找到了替代品,以下是有效的:

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

        instance.props = props;
        instance.context = state; …
Run Code Online (Sandbox Code Playgroud)

javascript prototypal-inheritance reactjs es6-class

2
推荐指数
1
解决办法
674
查看次数