我在ReactNative中添加产品和打开模态时遇到问题。
以下按钮
<Button style={styles.errorButtonStyle} onPress={this.onAddToCartPress.bind(this, this.props.menuItem)}>
IN CART
</ResetButton>
调用此功能
onAddToCartPress(){
textModal = 'Prodotto aggiunto'+"\n"+'al carrello';
toCart = true;
toAdd = true;
this.props.openModal(textModal, toCart, toAdd, this.props.closeModal.bind(this), this.onGoToCart.bind(this));
this.props.addToCart(this.props.menuItem, this.props.cartItems);
}
依次设置我的模态所需的变量并执行2个操作:
打开模式
export const openModal = (modalText, isForCart, isAdd, onPressClose, onPressCart) => {
return {
type: OPEN_MODAL,
modalText: modalText,
isForCart: isForCart,
isAdd: isAdd,
onPressClose: onPressClose,
onPressCart: onPressCart
};
};
将产品添加到购物车
export const addToCart = ( item, cartItems ) => {
return (dispatch) => {
let total = 0.00;
if(cartItems.length > 0){
let find = false;
for(var i = 0; i < cartItems.length; i++){
if(cartItems[i].id === item.id){
total += (cartItems[i].qty+1) * cartItems[i].price;
find = true;
} else {
total += cartItems[i].qty * cartItems[i].price;
}
}
if(find){
addQtyToItemCart( dispatch, item, total);
}else{
item.qty = 1;
total += item.qty*item.price;
addItemToCart( dispatch, item, total);
}
}else{
total += item.price;
item.qty = 1;
addItemToCart( dispatch, item, total );
}
};
};
其中又包括
const addItemToCart = (dispatch, item, total) => {
dispatch({
type: ADD_TO_CART ,
payload: item,
total: total
});
};
const addQtyToItemCart = (dispatch, item, total) => {
dispatch({
type: ADD_QTY_TO_ITEM_CART ,
payload: item,
total: total
});
};
问题:我无法弄清楚为什么-当我单击按钮时-执行addToCart
之后又被重定向到初始“场景”。当我将addToCart
注释掉时,模态打开,我停留在正确的页面上。
有人发生了吗?