在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ react 觸發(fā)組件方法的問題

react 觸發(fā)組件方法的問題

import React, { Component } from 'react';
import propTypes from "prop-types"

class Record extends Component {
    constructor(props) {
        super(props);
        this.state = {
            edit: false
        }

        this.handleEdit = this.handleEdit.bind(this);
        this.handleUpdate = this.handleUpdate.bind(this);
    }
    render() {
        return this.state.edit
            ?
            <tr>
                <td><input type="text" defaultValue={this.props.record.date} className="form-control" /></td>
                <td><input type="text" defaultValue={this.props.record.title} className="form-control" /></td>
                <td><input type="text" defaultValue={this.props.record.amount} className="form-control" /></td>
                <td>
                    <button className="btn btn-primary mr-1" onClick={this.handleUpdate(this.props.record.id)}>update</button>
                    <button className="btn btn-outline-secondary" onClick={this.handleEdit}>cancel</button>
                </td>
            </tr>
            :
            <tr>
                <td>{this.props.record.date}</td>
                <td>{this.props.record.title}</td>
                <td>¥ {this.props.record.amount}</td>
                <td>
                    <button className="btn btn-primary mr-1" onClick={this.handleEdit}>edit</button>
                    <button className="btn btn-danger">delete</button>
                </td>
            </tr>
    }
    // method
    handleEdit() {
        console.log(22222)
        this.setState({
            edit: !this.state.edit
        })
    }
    handleUpdate(id) {
        console.log(id, 'handleUpdate');
    }
}

export default Record;

Record.propTypes = {
    id: propTypes.number,
    date: propTypes.string,
    title: propTypes.string,
    amount: propTypes.number
}

為什么在我點擊 handleEdit 方法的時候,會觸發(fā) handleUpdate 方法呢?
而且之后再點擊 handleUpdate方法會沒有反應
圖片描述

回答
編輯回答
心悲涼

onClick事件里你直接執(zhí)行了,當然渲染的時候會觸發(fā)
改成這樣

<button className="btn btn-primary mr-1" onClick={e => this.handleUpdate(this.props.record.id)}>update</button>
2017年11月21日 02:02