Reputation: 121
I'm trying to dispatch redux action in exported to react component function. Is it possible? Or what should I do instead of this?
import { addToCart } from "../redux/actions/productActions";
export const checker = (item, cart) => dispatch => {
let orderItem = {
...item
};
cart.filter(product => {
if (product.productId === item.id) {
orderItem = {
id: item.id,
quantity: product.quantity + item.quantity
};
dispatch(addToCart(orderItem));
} else {
dispatch(addToCart(orderItem));
}
});
};
Upvotes: 1
Views: 184
Reputation: 2925
In a React/Redux app, you should not dispatch actions outside of React components. And React components should only dispatch actions implicitly via mapDispatchToProps
( https://react-redux.js.org/using-react-redux/connect-mapdispatch#providing-a-mapdispatchtoprops-parameter ) or useDispatch
( https://react-redux.js.org/next/api/hooks#usedispatch ).
Upvotes: 1