onClick事件处理以调用该特定按钮上的函数

时间:2018-09-12 12:54:44

标签: reactjs event-handling

import React from 'react';
import './MenuCard.css';

class MenuCard extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            showButton: false,
            hideButton: true,
            aValue: 1,
            breads: [],
            category: [],
            ids: 0
        };
        this.onShowButton = this.onShowButton.bind(this);
    }

    onShowButton = (id) => {
        this.setState({
                showButton: !this.state.showButton, 
                hideButton: !this.state.hideButton
        }));
    }

    onValueIncrease = () => {
        this.setState({aValue: this.state.aValue + 1});
    }

    onValueDecrease = () => {
        this.setState({aValue: this.state.aValue - 1});
    }

 render() {

        return (
            <div>
                {this.state.category.map(types => {
                    return (<div>
                        <div className="menu-head">{types}</div>
                        < div className="container-menu">
                            {this.state.breads.map((d, id)=> {
                                if (d.category === types) {
                                    return (
                                        <div className="content">
                                            <div className="items"> {d.item_name}</div>
                                            <div className="prices"> {d.price} Rs.</div>
                                            <button id ={id} onClick={() => this.onShowButton(d.id)}
                                                    hidden={this.state.showButton}
                                                    className="add-menu-btn"> add
                                            </button>
                                            <span key={d.id} hidden={this.state.hideButton}>
                                                     <button id={d.id} className="grp-btn-minus"
                                                             onClick={this.state.aValue <= 1 ? () => this.onShowButton(d.id) : () => this.onValueDecrease(d.id)}>-
                                                     </button>
                                                    <input className="grp-btn-text" type="text"
                                                           value={this.state.aValue} readOnly/>
                                                    <button id={d.id} className="grp-btn-plus"
                                                            onClick={() => this.onValueIncrease(d.id)}>+
                                                    </button>
                                                </span>
                                        </div>
                                    )
                                }
                            })}
                        </div>
                    </div>)
                })}
            </div>
        )
    }

根据项目1,有多个按钮。

在这里,当我单击单个按钮全部更新时,问题是我只需要单击单个按钮即可进行单个更新2

1 个答案:

答案 0 :(得分:0)

您需要将数组中的值保持在状态下,即:

@eval(using $expr)

如果您随后需要设置状态,则可能如下所示:

values: [
  { id: 1, value: 20},
  { id: 2, value: 1}
]

要从状态获取值:

const values = Object.assign({}, this.state.values, { [id]: value })
this.setState({ values })