【发布时间】:2019-07-01 16:57:31
【问题描述】:
我想创建一个封装其他组件并在其渲染方法中使用 JSX 的功能组件。我在网上看到了很多关于如何使用类组件做到这一点的内容,但我很好奇它是否适用于功能组件。
function WrappedContent() {
return <p>I'm wrapped</p>
}
function Wrapper(WrappedComponent) {
return (
<div>
<p>Wrapped: </p>
<WrappedComponent />
</div>
)
};
function App() {
return (
<div>
<Wrapper>
<WrappedContent />
</Wrapper>
</div>
)
}
我猜这与子组件如何传递到 <Wrapper>(通过 props.children?)有关。
这是一个包含上述代码的代码框:https://codesandbox.io/embed/gifted-cray-bqswi
【问题讨论】:
-
下次注意,Stack Overflow 的 Stack Snippets 支持 React,包括 JSX; here's how to do one。使用现场 sn-ps 可以帮助人们帮助您。
标签: javascript reactjs