react的onClick和onclick
onClick 等事件,與原生 HTML 不同,on 之后第一個字母是大寫的!,如果將?onClick={this.handleClick}?換成?onclick={this.handleClick}?則點擊事件不再生效。
? <div id="example"></div>
? ? <script type="text/babel">
? ? ? var LikeButton = React.createClass({
? ? ? ? getInitialState: function() {
? ? ? ? ? return {liked: false};
? ? ? ? },
? ? ? ? handleClick: function(event) {
? ? ? ? ? this.setState({liked: !this.state.liked});
? ? ? ? },
? ? ? ? render: function() {
? ? ? ? ? var text = this.state.liked ? '喜歡' : '不喜歡';
? ? ? ? ? return (
? ? ? ? ? ? <p onClick={this.handleClick}>
? ? ? ? ? ? ? 你<b>{text}</b>我。點我切換狀態(tài)。
? ? ? ? ? ? </p>
? ? ? ? ? );
? ? ? ? }
? ? ? });
? ? ? ReactDOM.render(
? ? ? ? <LikeButton />,
? ? ? ? document.getElementById('example')
? ? ? );
2018-05-01
因為這是React識別回調(diào)函數(shù)的寫法,onclick是html識別。。。。環(huán)境不一樣 -? -