MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Higher Order Components


Higher Order Components ("HOC" in short) is a react application design pattern that is used to enhance components with reusable code. They enable to add functionality and behaviors to existing component classes.

A HOC is a pure javascript function that accepts a component as it's argument and returns a new component with the extended functionality.

Higher Order Component that checks for authentication



Let's say we have a component that should only be displayed if the user is logged in.

So we create a HOC that checks for the authentication on each render():

AuthenticatedComponent.js

import React from "react";
export function requireAuthentication(Component) {
 return class AuthenticatedComponent extends React.Component {
 /**
 * Check if the user is authenticated, this.props.isAuthenticated
 * has to be set from your application logic (or use react-redux to retrieve it from global
state).
 */
 isAuthenticated() {
 return this.props.isAuthenticated;
 }
 /**
 * Render
 */
 render() {
 const loginErrorMessage = (
 <div>
 Please <a href="/login">login</a> in order to view this part of the
application.
 </div>
 );
 return (
 <div>
 { this.isAuthenticated === true ? <Component {...this.props} /> :
loginErrorMessage }
 </div>
 );
 }
 };
}
export default requireAuthentication;


We then just use this Higher Order Component in our components that should be hidden from anonymous users:

MyPrivateComponent.js
import React from "react";
import {requireAuthentication} from "./AuthenticatedComponent";
export class MyPrivateComponent extends React.Component {
 /**
 * Render
 */
 render() {
 return (
 <div>
 My secret search, that is only viewable by authenticated users.
 </div>
 );
 }
}
// Now wrap MyPrivateComponent with the requireAuthentication function
export default requireAuthentication(MyPrivateComponent);


Simple Higher Order Component


Let's say we want to console.log each time the component mounts:

hocLogger.js

export default function hocLogger(Component) {
 return class extends React.Component {
 componentDidMount() {
 console.log('Hey, we are mounted!');
 }
 render() {
 return <Component {...this.props} />;
 }
 }
}

Use this HOC in your code:

MyLoggedComponent.js

import React from "react";
import {hocLogger} from "./hocLogger";
export class MyLoggedComponent extends React.Component {
 render() {
 return (
 <div>
 This component gets logged to console on each mount.
 </div>
 );
 }
}
// Now wrap MyLoggedComponent with the hocLogger function
export default hocLogger(MyLoggedComponent);


Conclusion

In this page (written and validated by ) you learned about React Higher Order Components . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React with Redux.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.