[英]How to create a container component to use it multiple times?
I have a react component I want to fill inside of another component .我有一个反应组件,我想填充另一个组件。 I want to be able to use this smooth scroll
component kind of as a container, that way I could use it inside of several different components and easily enclose them with the smooth scroll component
's html, css and js.我希望能够将这种smooth scroll
组件用作容器,这样我就可以在几个不同的组件中使用它,并轻松地将它们与smooth scroll component
的 html、css 和 js 一起使用。
Smooth scroll component:平滑滚动组件:
render() {
return (
<div className="smooth-scroll-viewport">
<div className="smooth-scroll-container">
NEW COMPONENT'S CONTENT
</div>
</div>
);
}
How I want to use it inside of another component:我想如何在另一个组件中使用它:
<SmoothScroll>
<div className="some-content">
test
</div>
</SmoothScroll>
How can I realize such behavior?我怎样才能意识到这种行为?
you can use children
props from React你可以使用 React 的children
props
render() {
return (
<div className="smooth-scroll-viewport">
<div className="smooth-scroll-container">
{this.props.children}
</div>
</div>
);
}
Now you will be able to use like现在你将能够使用像
<SmoothScroll>
<div className="some-content">
test
</div>
</SmoothScroll>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.