#this 是我想对道具执行 Dispacth() 操作的地方我相信我正确连接了组件。第一个参数将 state 映射到 props,第二个参数映射到 dispatch。这样当我分派操作然后记录 getState 时,我可以看到商店已更新。
import React from "react";
import { Modal, Button } from "react-bootstrap";
import { connect } from "react-redux"
function AddCart(props) {
console.log("AddCart Props", props);
function handleClick(e) {
e.preventDefault();
props.addtocard()
// props.addtocard((props.count * props.price))
}
return <Modal
{...props}
size="md"
aria-labelledby="contained-modal-title-vcenter"
centered
>
<Modal.Header closeButton
>
<Modal.Title id="contained-modal-title-vcenter">
Successful!
</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>
<b>count:{props.count} size:{props.size} x title:{props.title} price:{(props.count * props.price).toFixed(2)}</b> added to your cart!
</p>
</Modal.Body>
<Modal.Footer >
<Button className="edit-btn" onClick={props.onHide}>Close</Button>
<Button className="edit-btn" href="/cart" onClick={handleClick}>Go to Cart</Button>
</Modal.Footer>
</Modal>
}
function mapDispatchToProps(dispatch, props) {
return {
addtocard: () => dispatch({ type: "ADD_ITEM", price: (props.count * props.price) })
}
}
export default connect(null, mapDispatchToProps)(AddCart);
catspeake
相关分类