react.net 点击功能失败



我无法在 MVC 5 视图中通过 reactjs 和 react.net 启动一个简单的 onClick 事件。我编写的数据绑定和其他函数似乎工作正常。我不断收到一个错误,指出警报未定义,但它只是一个简单的 javascript 警报,没有任何意义。请参阅下面的代码:

class ClickTest extends React.Component {
    ClickMe(){
        alert('the button was clicked');
    };
    render() {
        return (
            <div onClick={this.ClickMe()}>
                Click Me
          </div>
        );
    }
}

为我的组件生成的代码如下:

// @hash v3-241CABCC1541735262CAFE84D809779D00FA7D7B
// Automatically generated by ReactJS.NET. Do not edit, your changes will be overridden.
// Version: 3.2.0 (build 0e1da66) with Babel 6.7.7
// Generated at: 2017/11/10 12:00:58 PM
///////////////////////////////////////////////////////////////////////////////
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var HomeComp = function (_React$Component) {
    _inherits(HomeComp, _React$Component);
    function HomeComp() {
        var _Object$getPrototypeO;
        var _temp, _this, _ret;
        _classCallCheck(this, HomeComp);
        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }
        return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(HomeComp)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = {
            pc: _this.props.pc
        }, _temp), _possibleConstructorReturn(_this, _ret);
    }
    _createClass(HomeComp, [{
        key: 'render',
        value: function render() {
            return React.createElement(
                'div',
                null,
                'Hello ',
                this.state.pc
            );
        }
    }]);
    return HomeComp;
}(React.Component);
var ClickTest = function (_React$Component2) {
    _inherits(ClickTest, _React$Component2);
    function ClickTest() {
        _classCallCheck(this, ClickTest);
        return _possibleConstructorReturn(this, Object.getPrototypeOf(ClickTest).apply(this, arguments));
    }
    _createClass(ClickTest, [{
        key: 'ClickMe',
        value: function ClickMe() {
            alert('the button was clicked');
        }
    }, {
        key: 'render',
        value: function render() {
            return React.createElement(
                'div',
                null,
                React.createElement(
                    'div',
                    { onClick: this.ClickMe },
                    ' Click Me '
                )
            );
        }
    }]);
    return ClickTest;
}(React.Component);

class ClickTest extends React.Component {
    ClickMe(){
        alert('the button was clicked');
    };
    render() {
        return (
            <div onClick={this.ClickMe.bind(this)}>
                Click Me
          </div>
        );
    }
}
ReactDOM.render(
  <ClickTest />,
  document.getElementById('container')
);
<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>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

将调用方法与作用域"this"绑定

 <div onClick={this.ClickMe.bind(this)}>
            Click Me
      </div>

您可以使用胖箭头自动绑定。

class ClickTest extends React.Component {
    ClickMe = () => {
        alert('the button was clicked');
    }
    render() {
        return (
            <div onClick={this.ClickMe}>
                Click Me
          </div>
        );
    }
}

错误也是,

您正在传递对函数的引用。你不应该打电话给它。它应该是this.ClickMe的,而不是this.ClickMe()

相关内容

  • 没有找到相关文章

最新更新