简体   繁体   中英

The function does not work when I click the button

My buttons:

<div className="ingrBtns">
    <button className="ingrBtn" 
            onClick={() => addRemoveIngredient('add','lettuce')}>
            +
    </button>
    <button className="ingrBtn" 
            onClick={() => addRemoveIngredient('remove','lettuce')}>
            -
    </button>
    <div className="price"> 
         &nbsp;&nbsp;&nbsp;${(state.lettuce*0.5).toFixed(2)}
    </div>
</div>

The function does not work when I click the button. Where is the problem ?

You must use the keyword "this"

<div className="ingrBtns">
    <button className="ingrBtn" onClick={() => this.addRemoveIngredient('add','lettuce')}>+</button>
    <button className="ingrBtn" onClick={() => this.addRemoveIngredient('remove','lettuce')}>-</button>
    <div className="fiyat"> &nbsp;&nbsp;&nbsp;${(this.state.lettuce*0.5).toFixed(2)}</div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM