簡體   English   中英

如何創建容器組件以多次使用它?

[英]How to create a container component to use it multiple times?

我有一個反應組件,我想填充另一個組件 我希望能夠將這種smooth scroll組件用作容器,這樣我就可以在幾個不同的組件中使用它,並輕松地將它們與smooth scroll component的 html、css 和 js 一起使用。

平滑滾動組件:

render() {
    return (
        <div className="smooth-scroll-viewport">
            <div className="smooth-scroll-container">

             NEW COMPONENT'S CONTENT

            </div>
        </div>
    );
}

我想如何在另一個組件中使用它:

<SmoothScroll>
   <div className="some-content">
      test
   </div>
</SmoothScroll>

我怎樣才能意識到這種行為?

你可以使用 React 的children props

render() {
    return (
        <div className="smooth-scroll-viewport">
            <div className="smooth-scroll-container">
              {this.props.children}
            </div>
        </div>
    );
}

現在你將能夠使用像

<SmoothScroll>
   <div className="some-content">
      test
   </div>
</SmoothScroll>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM