【问题标题】:react-instantsearch-dom:: Failed prop type: Invalid prop `hitComponent` of type `object` supplied to `Hits`, expected `function`react-instantsearch-dom:: 道具类型失败:提供给“Hits”的“object”类型的无效道具“hitComponent”,预期的“函数”
【发布时间】:2019-08-20 05:28:55
【问题描述】:

我为“Hit”创建了一个类组件,并将其添加到另一个文件中,在该文件中我使用了 angolia react-instantsearch“Hits”。 检查此代码::

<Hits
    hitComponent={Hit}
    className="mb-30"
    showLoadingIndicator
/>

但是给了我一个警告::

警告:失败的道具类型:无效的道具hitComponent 类型 object 提供给 Hits,预期为 function

我无法将“Hit”转换为功能组件。这里如何使用类组件值作为函数?

【问题讨论】:

  • 您是否正确导入Hit?你能和codeSandbox链接吗?这个hitComponent={()=&gt;&lt;div /&gt;} 是否显示相同的警告?

标签: reactjs react-instantsearch


【解决方案1】:

不确定,但您可以尝试类似的方法:只需将一个函数包装在它周围..一个返回组件的函数

hitComponent={(props) => <Hit {...props} />}

   functions _Hit(props)
    {
    return <Hit {...props} />
    }
     hitComponent={_Hit}

【讨论】:

  • 两个版本都可以工作,但在第一个版本中你忘记了 props 作为函数的参数:hitComponent={(props) =&gt; &lt;Hit {...props} /&gt;}
  • @GuiTeK 感谢您的更新,已在答案中修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 2019-06-14
  • 2017-12-12
  • 1970-01-01
  • 2020-02-29
  • 2022-11-11
  • 2021-07-27
相关资源
最近更新 更多