【发布时间】:2021-01-26 11:26:21
【问题描述】:
我的目的是制作一个简单的高阶组件,其中我有一个可重用的 Section 组件,并且我可能会定义一些 css 类和 <section> 标签:
import React from 'react'
function SectionWrapper(props) {
return <section>{props.children}</section>
}
export default SectionWrapper
现在在我的父组件中,我可以像这样包装我的子部分组件:
<SectionWrapper>
<SectionTitle />
</SectionWrapper>
<SectionWrapper>
<List />
</SectionWrapper>
有没有更简洁的方法来实现这一点?例如。如何将子组件作为道具传递?
【问题讨论】:
-
您可以将其转换为 HOC,但是对于这样一个简单的示例,它不会将任何新的道具或行为注入到它所包装的内容中,我不太明白包装器作为 HOC 的意义.
标签: reactjs react-props react-functional-component higher-order-components