Réagir ajouter un événement de clic, modifier ce point

  • méthode 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
  • méthode 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
  • méthode 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
Publié 24 articles originaux · a gagné les éloges 4 · Vues 4456

Je suppose que tu aimes

Origine blog.csdn.net/Amo__/article/details/101553512
conseillé
Classement