react-redux购物车示例

时间:2018-08-09 10:10:49

标签: reactjs redux react-redux

我是新来的react和react-redux。我正在学习有关购物车的redux示例,这是链接enter link description here

我有两个问题:

  1. containers/ProductsContainer.js中,connect像这样通过了{ addToCart } import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { addToCart } from '../actions' import { getVisibleProducts } from '../reducers/products' import ProductItem from '../components/ProductItem' import ProductsList from '../components/ProductsList' ...... export default connect( mapStateToProps, { addToCart } )(ProductsContainer) 我不知道{ addToCart }如何传递到connect

2。addToCard像这样:

export const addToCart = productId => (dispatch, getState) => {
  if (getState().products.byId[productId].inventory > 0) {
     dispatch(addToCartUnsafe(productId))
  }
 }

getState参数来自哪里?

1 个答案:

答案 0 :(得分:1)

连接功能的定义是 export default connect( mapStateToProps, mapDispatchToProps )(SomeComponent)

此处{ addToCart }直接传递,代替了mapDispatchToProps。就像{addToCart : addToCart },称为速记语法。