我正在创建简单的东西,以寻求将按钮单击事件捕获到某些文本或获得一些警报。ReactJS JSX 代码粘贴在下面:
var SearchBar = React.createClass({
getInitialState: function() {
return {message: "test"};
},
test1: function(e) {
alert('hi');
this.setState({message: "New Message"});
},
render: function() {
var self = this;
return (
<div>
<button onClick={self.test1}>Change Message</button>
{this.state.message}
</div>
);
},
});
SearchBar
我在 MVC cshtml 中使用上述组件作为:
@Html.React("SearchBar", new{ })
按钮在 html 页面上呈现,但无法更改this.state.message
单击事件的值。我在哪里做错了?