High Order Component (HOC) & Custom Hook

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import WithLog from "./WithLog";
import reportWebVitals from "./reportWebVitals";

ReactDOM.render(
    <WithLog name="hello"/>,
  document.getElementById("root")
);
  • In traditional, we use high order the component to share the logic and variable

import React, {useState, useEffect} from 'react';

const WithLog = (WrappedComponent) => 

    class extends React.Component{
        constructor(props){
            super(props);
        }
        componentDidMount(){
            console.log(this.props.name);
        }
        render(){
            return (
            <WrappedComponent 
                name={this.props.name}
            />
            )
        }
    }
const Single = ({name}) => {
    return (
        <div>
            {name}
        </div>
    )
}

export default WithLog(Single);
  • However , we cannot mix it with hook, as useEffect cannot be called in callback

  • Now, we will use custom hook to replace it

Last updated

Was this helpful?