我是新来的react和react-redux。我正在学习有关购物车的redux示例,这是链接enter link description here
我有两个问题:
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
参数来自哪里?
答案 0 :(得分:1)
连接功能的定义是
export default connect(
mapStateToProps,
mapDispatchToProps
)(SomeComponent)
此处{ addToCart }
直接传递,代替了mapDispatchToProps。就像{addToCart : addToCart }
,称为速记语法。