【问题标题】:Error: Objects are not valid as a React Child错误:对象作为 React Child 无效
【发布时间】:2018-08-24 09:59:37
【问题描述】:

如果条件为假,我想显示一个对象,我在有状态组件返回语句中为此做了类似的事情。

{ !this.state.searchCoin ?   {displayCrypto}   : null }

为此,它抛出以下错误

对象作为 React Child 无效

我的显示加密看起来像这样(在渲染中调用)

let displayCrypto = CryptoData.map(el => {
    return (<CoinCard
       no={i++}
       key={el["short"]}
       coinShortName = {el["short"]}
       coinName = {el["long"]}
       coinPrice = {el["price"].toFixed(2)}
       marketCap = {(el["mktcap"]/1000000000).toFixed(4)}
       percentChange = {el["perc"].toFixed(2)}
       vwapData={el["vwapData"].toFixed(2)}
      />

[问题:]如果条件为假,我们如何使用行程表达式显示在对象上?

Ps:如果您对这个问题投了反对票,那么也请发表评论,以便我也可以改进我的问题。

【问题讨论】:

  • 您可能希望包含更多的上下文代码。 iel 来自哪里 displayCrypto
  • 删除{} -> { !this.state.searchCoin ? displayCrypto : null }
  • @OzzyWalsh i 只是增加其值的变量。 el 来自地图功能
  • @OlivierBoissé 您的评论让我意识到自己的错误并解决了我的问题。在答案中分享,我会将其标记为已回答。非常感谢冠军:)

标签: reactjs react-native


【解决方案1】:

只需删除 {} : { !this.state.searchCoin ? displayCrypto : null }

【讨论】:

    【解决方案2】:

    您将cryptoCoinCard 组件包装在括号中,这会提示错误。

    删除{},它应该可以工作。

    { !this.state.searchCoin ? displayCrypto : null }
    

    【讨论】:

      猜你喜欢
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 2021-08-20
      • 2021-08-05
      • 2016-10-26
      • 1970-01-01
      相关资源
      最近更新 更多