Skip to content

Instantly share code, notes, and snippets.

View RichieAHB's full-sized avatar

Richard Beddington RichieAHB

View GitHub Profile
@RichieAHB
RichieAHB / prop-drilling.md
Last active November 3, 2023 23:50
React "prop-drilling"

React "prop-drilling"

Having worked with React since the early days, I've seen a lot of ways or writing and structuring components. Whether it's the Component / Container (aka. smart / dumb) pattern, the state-management wars of the late teens, the sprinkling of Contexts everywhere or obviously hooks there's always some debate between teammates about libraries, lint rules or patterns. However, the one thing that's been there, quietly, for a long time now is the term "prop drilling".

What is prop-drilling

Prop drilling is the idea of having to push a prop through a large component tree right down to a node that is many components deep through layers of components that don't care about that prop. For example:

const FilterButton = ({ onClick, ['aria-label']: filterButtonAriaLabel }) => (
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@RichieAHB
RichieAHB / Hooks.js
Last active October 26, 2018 15:09
import { hook, useState, useEffect } from 'react';
import { myMemoize } from './utils/my-memoize';
import CountsAPI from './services/count-api';
const Comp = (props, [counter, setCounter], [name, setName]) => <div />;
// hook(...stateful)(...effectful)
/*
* statefuls are functions that have access to props (and maybe the previous state?), reducers could be here too. What they return could be a variety of things but they allow react internals to pass the the value and updater to the following hooks and component
@RichieAHB
RichieAHB / index.js
Created July 24, 2018 11:16
Coding challenge
const crypto = require("crypto");
const memoize = require("fast-memoize");
const levenshtein = require("js-levenshtein");
const createInts = n => Array.from({ length: n }, (_, i) => i);
const trace = memoize(i =>
crypto
.createHash("sha1")
.update(`${i}`)
import React from 'react';
import { render } from 'react-dom';
import memoize from "lodash.memoize";
const somethingExpensive = iters => {
const arr = [];
for (let i = 0; i < iters; i += 1) {
arr.push(Math.random());
arr.sort(); // ¯\_(ツ)_/¯
}
@RichieAHB
RichieAHB / naive-algebraic-effect.js
Last active December 19, 2017 09:28
An attempt at understanding and implementing a naive model for algebraic effects in JS.
const Effect = (type, val) => ({ type, val });
const run = gen => {
let r;
while (!(r = gen.next()).done) {}
return r.value;
};
function* handle(fn, map = {}) {
const gen = fn();
<?php if ( function_exists( 'have_rows' ) && have_rows( 'page_section' ) ) : ?>
<?php while ( have_rows( 'page_section' ) ) : the_row(); ?>
<?php get_template_part( 'content', 'page-section' ); ?>
<?php endwhile; ?>
<?php if (is_front_page() && count( get_field( 'page_section' ) ) > 1) : ?>
@RichieAHB
RichieAHB / SassMeister-input.scss
Created September 16, 2014 13:45
Generated by SassMeister.com.
// ----
// Sass (v3.4.4)
// Compass (v1.0.1)
// ----
$px-only: true;
$pixelBase : 16; /* 1 */
$visual-grid-breakpoints: '';
$grid-columns: 12;