简体   繁体   English

如何仅更改React组件的渲染

[英]How to change only render of a React component

I want to use react-id-swiper library which exports a component named Swiper . 我想用react-id-swiper其中出口被命名的组件库Swiper This is its render method: 这是其render方法:

render() {
    const { containerClass, wrapperClass, children, rtl } = this.props;
    const rtlProp = rtl ? { dir: 'rtl' } : {};

    return (
      <div className={containerClass} {...rtlProp}>
        {this.renderParallax()}
        <div className={wrapperClass}>
          {React.Children.map(children, this.renderContent)}
        </div>
        {this.renderPagination()}
        {this.renderScrollBar()}
        {this.renderNextButton()}
        {this.renderPrevButton()}
      </div>
    );
  }
}

This component perfectly matches my needs, except that I need to place pagination in an outer place (outside of the containerClass element). 该组件完全符合我的需求,除了我需要将分页放置在外部位置( containerClass元素之外)。

One possible solution is to inherit Swiper class and change only it's render method. 一种可能的解决方案是继承Swiper类并仅更改其render方法。 However Facebook docs are explicit to not use inheritance and use composition instead. 但是,Facebook文档明确不使用继承,而是使用合成。

What's the best way to move the pagination outside of containerClass ? 将分页移出containerClass的最佳方法是什么? Can it be done with composition? 可以用合成完成吗?

As it stands there is not much you can do, since this component is render specifically in this structure align with a secondary library. 就目前而言,您无能为力,因为此组件是在此结构中专门渲染的,与辅助库对齐。 In the rebuildSwiper function you can see it passing it the dom elements to this other library to bind and handle all interactions. rebuildSwiper函数中,您可以看到它将dom元素传递给该另一个库来绑定和处理所有交互。

Depending on the flexibility of the secondary app you could move the render order. 根据辅助应用程序的灵活性,您可以移动渲染顺序。 To do so I would first evaluate what the capabilities are of the inner library, then fork this repo and publish updates using the a scoped package such as @<username>/react-id-swiper . 为此,我首先要评估内部库的功能,然后派生此存储库,并使用范围限定的包(例如@<username>/react-id-swiper发布更新。

I think composition would be well and good, but because you're using a library and not able to design the source class, I think inheritance is your only option. 我认为组合会很好,但是由于您使用的是库并且无法设计源类,因此我认为继承是唯一的选择。

I would recommend simply extending the Swiper class. 我建议简单地扩展Swiper类。 You can write your new render method in a way that would allow for composition reuse moving forard if you want to follow these recommended practices , but I think it's safe to trust your gut in this case. 如果您要遵循这些建议的做法 ,则可以以允许重新使用合成的方式编写新的render方法,但是在这种情况下,我相信您的直觉是安全的。

I'll be watching this question and am curious to see how others would approach it and what you're ultimate take is, too. 我将关注这个问题,并且很好奇其他人将如何解决这个问题,以及您的最终目标是什么。

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

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