Skip to content

Instantly share code, notes, and snippets.

@nikparo
nikparo / Effect.jsx
Created February 18, 2021 09:04
Running React parent effects before child effects
// Sometimes you want to run parent effects before those of the children. E.g. when setting
// something up or binding document event listeners. By passing the effect to the first child it
// will run before any effects by later children.
export function Effect({ effect }) {
useEffect(() => effect?.(), [effect]);
return null;
}
//
@nikparo
nikparo / skip-same-state-enhancer.js
Last active October 14, 2020 10:26
Skip redux subscription updates when state hasn't changed
import { createStore as basicCreateStore } from 'redux';
export const skipSameStateEnhancer = createStore => (
reducer,
initialState,
enhancer,
) => {
const store = createStore(reducer, initialState, enhancer);
// Create an in-between store so that we can skip subscription updates where nothing changed.
@nikparo
nikparo / styleMemo.js
Created October 6, 2020 18:20
React.memo: Shallowly compare props except for `style`, which is best shallowly compared on its own.
import React from 'react';
import shallowEqual from 'shallowequal';
function customizer(valueA, valueB, propName) {
if (propName === 'style') {
return shallowEqual(valueA, valueB);
}
return undefined;
}