简体   繁体   English

检查从功能状态redux组件中的mapStateToProps传递的道具的真实性

[英]Check truthiness of props passed from mapStateToProps in functional react redux component

I have 3 components, TypeList , ConnectedType ( =connect(mapStateToProps)(Type ), and Type . Type will receive props from both TypeList ( onClick, name ) passing props to ConnectedType as well as ConnectedType's mapStateToProps ( onMiniPokemonClick, miniPokemon ). How do I do a check to see if miniPokemon exists before mapping it out? Is it possible to do logic on a functional component or do I have to make it a class and create a helper function inside? 我有3个组成部分,TYPELIST,ConnectedType( =connect(mapStateToProps)(Type )和类型 。类型将收到来自TYPELIST道具( onClick, name )通过道具来ConnectedType以及ConnectedType的mapStateToProps( onMiniPokemonClick, miniPokemon )。怎么办我miniPokemon映射之前进行检查,是否可以在功能组件上进行逻辑处理,还是必须使其成为一个类并在其中创建一个辅助函数?

const Type = ({onClick, name, onMiniPokemonClick, miniPokemon}) => (
  <li
    onClick={onClick}
  >
    {name}
    <ul>
      {
        if (miniPokemon) {
          miniPokemon.map(function (pokemon, idx) {
            return (<MiniPokemon onClick={() => onMiniPokemonClick(pokemon.name)} name={pokemon.name}/>)
          })
        }
      }
    </ul>
  </li>
)

All you need to do is use brackets to allow a multiline function body 您需要做的就是使用方括号允许多行功能主体

const Type = ({onClick, name, onMiniPokemonClick, miniPokemon}) => {
   // some logic   
   return (
      <li
        onClick={onClick}
      >...
      </li>
   )
}

Or if you just need to ensure that miniPokemon is an array when a null/undefined value comes through - you could supply a default argument: 或者,如果仅需要确保在miniPokemon null / undefined值时miniPokemon是一个数组,则可以提供一个默认参数:

({onClick, name, onMiniPokemonClick, miniPokemon = []})

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

相关问题 React Redux 无法传递功能组件的 props - React Redux cannot pass props of a functional component 数据未通过容器中的 React-Redux(来自 mapStateToProps)传递到组件视图 - Data not getting passed to View of Component using React-Redux (from mapStateToProps) in Container 道具的打印值,由mapStateToProps从redux传递到组件 - Print value from props, which is delivered to the component from redux by mapStateToProps 使用mapStateToProps()提供的Redux道具设置组件的本地状态 - Set local state of component with Redux props given from mapStateToProps() 从功能组件中传递的道具映射 - Mapping from passed props in a functional component 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect? React Redux - 混淆使用函数组件与 useDispatch 与类组件和 mapStateToProps - React Redux - Confusion on Using Functional Component with useDispatch vs Class Component and mapStateToProps React / Redux:mapStateToProps实际上并没有将状态映射到props - React / Redux: mapStateToProps not actually mapping state to props React Redux不同步mapStateToProps中的道具 - React redux do not sync props in mapStateToProps React redux mapStateToProps 未设置返回到我的道具 - React redux mapStateToProps not set return to my props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM