Skip to content

Instantly share code, notes, and snippets.


Grady Kuhnline heygrady

View GitHub Profile
heygrady /
Last active Jan 13, 2021
Redux module patterns: app state, view state and regional state

Redux module patterns

app state, view state and regional state

separating components and containers

and code-splitting reducers


Redux JSONAPI Resources


  1. Store resources in redux following the JSONAPI resource spec.
  2. Provide actions and thunks for managing resources similar to what Ember Data Models support.

Let's take an example of a building that has floorplans.


I was hacking my custom middleware into the webpack-dev-server like this:


// start.js
// use custom serverSideRender middleware with webpack-dev-server
const serverSideRenderMiddleware = require('./ssr-middleware')

// Copy the defaultFeatures logic from webpack-dev-server. We need to hack the
heygrady /
Last active Nov 16, 2020
Refactoring derived state

How to refactor derived state

In our aging react code base we had been using componentWillRecieveProps for situations where we wanted to recalculate "stuff" when props change. Recent versions of react renamed this method to UNSAFE_componentWillRecieveProps for a variety of reasons. We recently went through an exercise where we tried to kill UNSAFE_componentWillRecieveProps once and for all.

The following is a contrived example of how to refactor such a component.

Step 1: How not to do it.

Here's a button component that uses UNSAFE_componentWillReceiveProps to keep its internal state in sync with the external props.


Binding handlers

Binding specific values to events is a common occurrence in a React application. Let's kick this off with a common example. Here we have a <Button /> that calls a click handler with a specific argument: entity. There are several things wrong here.

Note: we will be refactoring this example as we go along.

  1. We're binding a function in the render method
  2. We're binding two props together in the child component
  3. We're breaking with several established naming conventions
  4. We're using a class component when a functional component will suffice
heygrady /
Last active Feb 24, 2019
Core concepts of redux modules

Redux modules

Our applications have grown organically and our current collection of actions, reducers and selectors are due for an upgrade. This document is an attempt to outline an better way to organize our redux code.

The biggest changes here are the introduction of "modules" and redux-sagas.

Core concepts

  • modules — a grouping of actions, constants, reducers, etc. that all deal with the same portion of the state.
  • actions — action creators
heygrady /
Last active Jan 27, 2021
Avoiding HOC; Favoring render props
heygrady /
Last active Nov 25, 2018
Lazy loading (using IntersectionObsever) in a React app

Lazy loading (using IntersectionObsever) in a React app

The Lighthouse performance audit tool recommends lazy-loading offscreen images (and other content)

They go a step further and recommend using the new IntersectionObserver API to manage offscreen content. Of course, you need to use a polyfill to target old browsers (core-js doesn't offer a polyfill).

The documentation provided by the Lighthouse team warns against over-applying the IntersectionObserver. The have a whole section devoted to explaining why you don't want to [intersect all the things](

heygrady /
Last active Apr 7, 2020
Rules for crafting good components

Rules for crafting good components

Here are some good rules for a high-quality react-redux app.

We're in the process of reworking our app to make it more maintainable. In a previous meeting we discussed the general rules of thumb for writing maintainable code. Here we're going to be covering rules of thumb for writing good components.

  1. Components should do one thing.
    • We use BEM as a guide.
    • A nested "block" should be a standalone component.
  2. Prefer stateless functional components.
heygrady / start-server.js
Last active Jun 5, 2020
create-react-app ssr dev script
View start-server.js
// @remove-on-eject-begin
* Copyright (c) 2015-present, Facebook, Inc.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
// @remove-on-eject-end
'use strict';