【问题标题】:How can I pass function that return component as props?如何将返回组件的函数作为道具传递?
【发布时间】:2018-06-30 19:05:12
【问题描述】:

我需要创建自定义列表项的轮播,并且我已经将轮播组件库之一(react-native-snap-carousel)导入到项目中,但是轮播组件需要返回我们需要的组件的道具呈现为轮播 itemList。 所以我发送这样的道具

[carousel {:render-item (fn [] [my-custom-item]) 
               ...otherprops... }]

但是这条消息出错了

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

如何将道具作为返回元素的函数发送?

这是在js上调用轮播组件的方法

        <Carousel
          ref={(c) => { this._carousel = c; }}
          data={this.state.entries}
          renderItem={this._renderItem}
          sliderWidth={sliderWidth}
          itemWidth={itemWidth}
        />

【问题讨论】:

    标签: carousel clojurescript reagent


    【解决方案1】:

    Carousel 组件期望从 render-item 函数返回一个反应元素或字符串。但是您返回的是普通试剂成分。

    您需要使用试剂的as-element 函数将试剂成分转换为反应成分:

    [carousel {:render-item (fn [] (reagent/as-element [my-custom-item])) 
               ...otherprops... }]
    

    【讨论】:

      猜你喜欢
      • 2020-08-28
      • 1970-01-01
      • 2017-10-31
      • 2022-07-19
      • 2018-08-01
      • 2019-07-03
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多