简体   繁体   English

React Redux嵌套的智能组件-状态未映射到道具

[英]React redux nested smart component - state isn't mapped to props

I am creating a drinks counter that will count the amount of drinks consumed as part of a project to learn redux. 我正在创建一个饮料计数器,该计数器将计算学习Redux的项目中消耗的饮料量。

I have some initial state that has a drinks property that contains a collection of drinks and their current count: 我有一些初始状态,其饮料属性包含饮料的集合及其当前计数:

 //State { drinks: { coffee: 0, water: 0, } //... } 

I have created a Counter presentational component: 我创建了一个Counter演示组件:

 import React from 'react'; import PropTypes from 'prop-types'; import Button from './Button'; const ucFirst = string => string.charAt(0).toUpperCase() + string.slice(1); const Counter = ({name, count, onIncrement, onDecrement}) => ( <div className="counter"> <h2>{ucFirst(name)}</h2> <span>{count}</span> <Button onClick={() => {onIncrement(name)}}>+</Button> <Button onClick={() => {onDecrement(name)}}>-</Button> </div> ); Counter.propTypes = { name: PropTypes.string.isRequired, count: PropTypes.number.isRequired, onIncrement: PropTypes.func.isRequired, onDecrement: PropTypes.func.isRequired, } export default Counter; 

Followed by a DrinkCounter container component: 随后是DrinkCounter容器组件:

 import { connect } from 'react-redux' import { incrementDrink, decrementDrink } from '../actions' import Counter from '../components/Counter' const mapStateToProps = (state, ownProps) => ({ count: state.drinks[ownProps.drink], name: ownProps.drink }) const mapDispatchToProps = dispatch => ({ onIncrement: drink => dispatch(incrementDrink(drink)), onDecrement: drink => dispatch(decrementDrink(drink)) }) export default connect( mapStateToProps, mapDispatchToProps )(Counter) 

This works as expected, if I add 如果我添加,这将按预期工作

<DrinksContainer name="coffee" />

into my App component. 进入我的App组件。

Now this is where the problem begins, I want to create two more components one presentational and one container that will map over the keys of my drinks object and output a DrinkCounter for each, I have the following code but I'm getting the error "Failed prop type: The prop name is marked as required in Counter , but its value is undefined " 现在,这是问题的开始的地方,我要创建两个组件一个表象和一个容器,将映射的钥匙我的drinks对象和输出DrinkCounter每个,我有以下的代码,但我得到的错误“道具类型失败:道具nameCounter标记为必需,但其值undefined

Here is List container: 这是List容器:

 import React, {Fragment} from 'react' import PropTypes from 'prop-types' import DrinkCounter from '../containers/DrinkCounter' const List = ({drinks}) => { return ( <Fragment> { Object.keys(drinks).map( (drink, index) => (<DrinkCounter name={drink} key={index} />) ) } </Fragment> ) } List.propTypes = { drinks: PropTypes.object.isRequired } export default List 

and here is the DrinksList container: 这是DrinksList容器:

 import React from 'react'; import {connect} from 'react-redux'; import List from '../components/List'; const mapStateToProps = state => ({ drinks: state.drinks }) export default connect(mapStateToProps)(List); 

Could someone explain where I'm going wrong? 有人可以解释我要去哪里错吗?

我已经想通了这个问题,在List组件,我给了错误的道具DrinkCounter我传递name时,实际上它应该是drink

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM