react.net onclick功能失败

时间:2017-11-10 08:40:27

标签: reactjs asp.net-mvc-5 reactjs.net

我无法在MVC 5视图中通过react.net与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);

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

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

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

错误也是,

您正在传递对您的功能的引用。 You should not call it。它应该是this.ClickMe而不是this.ClickMe()