この点を変更、クリックイベントを追加し反応

  • 方法1
import React, { Component } from 'react'

class ClickComponent extends Component {
    constructor(props){
        super(props)
        this.state ={
            msg: '这是一个标题'
        }
    }
    run() {
        alert(this.state.msg)
    }
    render() {
        return (
        <div>
            <button onClick={this.run.bind(this)}>请点击</button>
        </div>
        )
    }
}

export default ClickComponent
  • 方法2
import React, { Component } from 'react'
class ClickComponent extends Component {
    constructor(props){
        super(props)
        this.state ={
            msg: '这是一个标题23'
        }
        this.getClick = this.getClick.bind(this);
    }
    getClick() {
        alert(this.state.msg)
    }
    render() {
        return (
        <div>
            <button onClick={this.getClick}>请点击</button>
        </div>
        )
    }
}

export default ClickComponent
  • 方法3
import React, { Component } from 'react'
class ClickComponent extends Component {
    constructor(props){
        super(props)
        this.state ={
            msg: '这是一个标题233'
        }
    }
    getClick=()=>{
        alert(this.state.msg)
    }
    render() {
        return (
        <div>
            <button onClick={this.getClick}>请点击</button>

        </div>
        )
    }
}

export default ClickComponent
公開された24元の記事 ウォンの賞賛4 ビュー4456

おすすめ

転載: blog.csdn.net/Amo__/article/details/101553512