Skip to content

Instantly share code, notes, and snippets.

View Jordan-Gilliam's full-sized avatar
🎷
jazzascriptn

Nolansym Jordan-Gilliam

🎷
jazzascriptn
View GitHub Profile
const not = fn => (...args) => !fn.apply(null, args);
const isZero = (extState, evtObj) => evtObj.key === 0;
const isNotZero = not(isZero);
const isMinus = (extState, evtObj) => evtObj.operator === '-';
const isNotMinus = not(isMinus);
const divideByZero = (extState, evtObj) =>
extState.operand2 === '0.' && extState.operator === '/';
const notDivideByZero = not(divideByZero);
const calcMachine = Machine({
id: 'calculator',
@Jordan-Gilliam
Jordan-Gilliam / doubleUseEffect.js
Last active February 17, 2020 09:23
a data fetching use case for useCallback
function SearchResults() {
// 🔴 Re-triggers all effects on every render
function getFetchUrl(query) {
return 'https://hn.algolia.com/api/v1/search?query=' + query;
}
useEffect(() => {
const url = getFetchUrl('react');
// ... Fetch data and do something ...
}, [getFetchUrl]); // 🚧 Deps are correct but they change too often
@Jordan-Gilliam
Jordan-Gilliam / useReducerCounter.js
Last active February 17, 2020 08:58
When you find yourself writing setSomething(something => ...), it’s a good time to consider using a reducer instead. .
// src: https://overreacted.io/a-complete-guide-to-useeffect/
// Optimized useReducer Counter
/*
“How is this any better?”
The answer is that React guarantees the dispatch function to be constant throughout the component lifetime.
So the example above doesn’t ever need to resubscribe the interval.
*/
/*
Explanation:
@Jordan-Gilliam
Jordan-Gilliam / makeSingleton.js
Created February 4, 2020 05:16
Create a true singleton with es6 Proxies
function makeSingleton(func) {
let instance,
return new Proxy(func, {
construct: function (target, args) {
if (!instance) {
instance = new func();
}
return instance;
}
});