in Technology by
What are Higher Order Components in React js?

1 Answer

0 votes
by
Simply put, Higher Order Component(HOC) is a function that takes in a component and returns a new component.
 
image
When do we need a Higher Order Component?
While developing React applications, we might develop components that are quite similar to each other with minute differences.
In most cases, developing similar components might not be an issue but, while developing larger applications we need to keep our code DRY, therefore, we want an abstraction that allows us to define this logic in a single place and share it across components.
HOC allows us to create that abstraction.
Example of a HOC:
Consider the following components having similar functionality
The following component displays the list of articles:
class ArticlesList extends React.Component{
 constructor(props){
   super(props);
   this.handleChange = this.handleChange.bind(this);
   this.state = {
     articles : API.getArticles();
   }
 }

  componentDidMount() {
   //Listen to changes made in the API
   API.addChangeListener(this.handleChange);
 }

 componentWillUnmount() {
   // Clean up listener
   API.removeChangeListener(this.handleChange);
 }

 handleChange() {
   // Update the articles variable whenever the API changes
   this.setState({
     articles: DataSource.getArticles()
   });
 }

 render(){
   return(
     <div>
       {this.state.articles.map((article)=>(
         <ArticleData article={article} key={article.id}/>
       ))}
     </div>
   )
 }
}

The following component displays the list of users:<div style="box-sizing: border-box; margin: 0px; padding: 0.2em 0.6em; border-width: 0.1em 0.1em 0.1em 0.8em; border-style: solid; border-color: gray; border-image: initi

Related questions

0 votes
    Why are reactions of higher order less in number? Select the correct answer from above options...
asked Jan 4, 2022 in Education by JackTerrance
0 votes
    If page size of 1MB memory is 1 KB then the number of higher order bits of address bus used to denote page number is ( ... (c) 12 (d) 9 Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    What are error boundaries in React js?...
asked Dec 5, 2020 in Technology by JackTerrance
0 votes
    I am using Firebase authentication and react-redux in my app. I added firebase.auth().onAuthStateChanged to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am using Firebase authentication and react-redux in my app. I added firebase.auth().onAuthStateChanged to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I'm trying to use the sensor's API with react and I can't seen to be able to make it work ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I'm trying to use the sensor's API with react and I can't seen to be able to make it work ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Currently working on my portfolio. I used this template. I'm trying to make my profile image into a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do should be something extremly simple but still I cannot seem to get it done ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how do I become a react JS developer from scratch? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what is the salary of a React JS developer in Canada? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what does a React JS developer do? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how hard is it to be React JS Developer? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
...