Skip to content

Instantly share code, notes, and snippets.

View bradwestfall's full-sized avatar

Brad Westfall bradwestfall

View GitHub Profile
@bradwestfall
bradwestfall / HoC-vs-RenderProps-vs-Hooks.md
Last active April 14, 2024 15:54
An explanation of why Hooks are a nicer way to abstract re-useable state and functionality vs HoC's and Render Props

HoC (pattern) vs Render Props (pattern) vs Hooks (not pattern, a new API)

Someone was asking me about comparing the HoC and Render Props patterns (and their shortcomings) to hooks. I might leave this up as a public gist for others if it's helpful.


tldr;

Issues with HoC:

import React, { useState, useRef } from 'react'
import ReactDOM from 'react-dom'
/**
* 1. Start by creating some state to store an array of items
* where you have the array holding objects as shown below
* 2. Eventually, your array will start empty, but for now it might
* make sense to have that array already populated with an item
* or two so that way we can show them in the `<ul>`. So do that
* and then iterate over the `items` array in the JSX and make