Higher Order Components (HOCs) are the coined term for a custom Component that accepts dynamically provided children. For example, let’s make <LazyLoad />
Component that takes child image tags as children
, waits until the <LazyLoad />
Component is scrolled into view, and then loads the images they point to in the background (before rendering them to the DOM).
An HOC accepts children via props:
DOM.render(
<LazyLoad>
<img src="https://media.giphy.com/media/HhvUpQhBWMtwc/200.gif"/>
<img src="https://media2.giphy.com/media/3oEduUDvycvu3GYkdG/200w.gif"/>
<img src="https://media0.giphy.com/media/142UITjG5GjIRi/200w.gif" />
</LazyLoad>,
document.body)
Creating an HOC means handling this.props.children
in the Component’s code:
interactive example can be found at https://goo.gl/ns0B6j
class LazyLoad extends Component {
constructor(p){
super(p)
this.state = { loaded:0 }
this._scroll = this._scroll.bind(this)
}
_scroll(){
let el = DOM.findDOMNode(this)
let {top} = el.getBoundingClientRect()
let viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
if(top < (viewportHeight + this.props.top)) {
window.removeEventListener('scroll', this._scroll)
this.setState({loaded:1})
}
}
componentDidMount(){
window.addEventListener('scroll', this._trackYPosition)
this._scroll()
}
componentWillUnmount(){
window.removeEventListener('scroll', this._trackYPosition)
}
render(){
let {children} = this.props,
{loaded} = this.state
return <div>
{loaded && children}
</div>
}
}
LazyLoad.defaultProps = {
top: 100
}
Noting a few things about this code:
this.state = {loaded: 0}
) in the constructor()
. This will be set to 1
when the parent container is scrolled into view.render()
returns the props.children
as child elements when this occurs. Extract the src
by using ES6 destructuring, where {props:{src}}
creates a variable src
with the appropriate value.componentDidMount()
lifecycle method. This is used because on mount, we’d like the component to check if the HOC is visible._scroll()
, grabs the HOC Component’s DOM element with DOM.findDOMNode()
and then gets the elements position. This position is compared to the height of the browser window, and if it is less than 100px from the bottom, then the scroll listener is removed and loaded
is set to 1
.This technique is called HOC (Higher Order Component) because we pass in elements as this.props.children
when we nest those elements inside the container component:
<HOC>
<div>some</div>
<span>children</span>
<Props/>
</HOC>
All of these nested elements (which can be custom components) are nested under <HOC/>
, thus HOC
’s code will be able to access them as this.props.children
.
If you want to explore more, visit our React.JS edu & tutorials section!
Below are some examples:
In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…
We’re thrilled to announce an exciting opportunity for you to win not one but two…
Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…
Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…
Another week to bring you the top yield platforms for three of the most prominent…
If you hold a large volume of LEDU tokens above 1 million units and wish…