Javascript 能否将子项添加到高阶组件中的WrappedComponent?
我正在尝试实现一个HOC,它在组件中添加了一个带有交叉点观察者的底部检查。为此,我需要在小时候添加一个底层哨兵 因此,在HOC内部,我需要做:Javascript 能否将子项添加到高阶组件中的WrappedComponent?,javascript,reactjs,higher-order-components,Javascript,Reactjs,Higher Order Components,我正在尝试实现一个HOC,它在组件中添加了一个带有交叉点观察者的底部检查。为此,我需要在小时候添加一个底层哨兵 因此,在HOC内部,我需要做: render () { const { children, ...otherProps } = this.props return ( <WrappedComponent {...otherProps}> {children} <BottomSentinel /> </WrapperComponent
render () {
const { children, ...otherProps } = this.props
return (
<WrappedComponent {...otherProps}>
{children}
<BottomSentinel />
</WrapperComponent>
}
render(){
const{children,…otherProps}=this.props
返回(
{儿童}
}
这不起作用。仅呈现WrappedComponent中定义为子元素的元素。DOM中不显示BottomSentinel
有人能帮我吗?在HOC中添加孩子不起作用吗?有更好的方法吗
仅呈现WrappedComponent中定义为子级的元素
可能您缺少在WrappedComponent
中渲染子对象的功能
function SomeWrappedComponent({children}) {
return (
<>
<WrappedComponentChildren />
...
{children} // You also need to render children inside WrappedComponent
</>
)
}
请展示您的完整组件/HOC以及如何使用它
render () {
const { children, ...otherProps } = this.props
return (
<>
<WrappedComponent {...otherProps} />
{children}
<BottomSentinel /> // rendering outside WrappedComponent
</>
}