Redux-如何在handleChange中获取更改数量的更新输入值



我提取了数据并显示了用户的购物车信息

现在我想更改输入字段中的产品数量。

但当我更改数量时,它总是得到存储在数据库中的值(请参阅handleChange(

默认值为1,但如果i更改为2(从输入值(,则应在handleChange函数中的常量数量中为2。

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { cartItems, removeCart, updateCart } from '../actions/cartAction';
class Cart extends Component {
constructor(props) {
super(props);
this.state = {

};
}
componentWillMount() {
let getValue = localStorage.getItem("userData");     
this.setState({ 
getValue: JSON.parse(getValue), 
}); 
}
componentDidMount() {
const emailData = this.state.getValue.email;
this.props.cartItems(emailData);
}
handleChange = (share) => (event) => {
this.setState({ [event.target.name]: event.target.value });
const cID = share._id;
const pprice = share.pprice;
const qty = share.qty;
const updateData = {cID: cID, pprice: pprice, qty: qty}
this.props.updateCart(updateData);
};
render() {
return (
<tbody>
{this.props.cartDetails.length && this.props.cartDetails.map(share => {
return ( 
<tr key={share._id}>
<td class="text-center">
<p> {share.pid} </p>
</td>
<th class="text-center" scope="row">
<img src="assets/img/product/2.jpg" alt="img" height="150px"/>
</th>
<td class="text-center">
<span class="whish-title">{share.pname}</span>
</td>
<td class="text-center">
<span class="whish-list-price">
{share.pprice}
</span>
</td>                                            
<td class="text-center">
<div class="product-count style">
<div class="count d-flex justify-content-center">
<input type="number" onChange={this.handleChange(share)} defaultValue={share.qty} name="qty" min="1" max="10" step="1"/>
</div>
</div>
</td>
<td class="text-center">
<span class="whish-list-price">
{share.total_price}
</span>
</td>
<td class="text-center">
<span class="whish-list-price">
<button onClick={this.deleteCart(share)} className="btn theme-btn--dark1 btn--md">Delete</button>
</span>
</td>
</tr>   
);
})}  
</tbody>   
) 
}
}    
const mapStateToProps = (state) => ({ cartDetails: state.cartDetails });
const mapDispatchToProps = {cartItems, removeCart, updateCart};
export default connect(mapStateToProps,mapDispatchToProps)(Cart); 

我认为问题出在上

const updateData = {cID: cID, pprice: pprice, qty: qty}

为什么使用share.qty而不是event.target.value?我也不理解的意义

this.setState({ [event.target.name]: event.target.value });

您在哪里使用这些值?

相关内容

  • 没有找到相关文章

最新更新