Skip to content

Instantly share code, notes, and snippets.

@mfunkie
mfunkie / useRevalidateInterval.ts
Last active March 8, 2023 00:08
useRevalidateInterval
import { useEffect } from 'react';
import { useRevalidator, useSearchParams } from 'react-router-dom';
export function useRevalidateInterval(
shouldRevalidate: boolean,
revalidateSearchParam: string,
intervalTimeoutMs = 3000
) {
const [searchParams, setSearchParams] = useSearchParams();
const revalidator = useRevalidator();
@mfunkie
mfunkie / useCallOnUnmount.js
Last active March 8, 2019 21:26
Invoke a callback only on unmount of the component
import React, { useEffect, useRef } from 'react'
// usage
function MyComponent() {
const { cleanupReduxStateWithSavedIDSoModalDoesntCloseNextTimeItIsOpened, savedID } = props
useEffect(() => {
if (savedID) {
// onSuccess could be doing something where in other areas savedID is still pulled from a redux store
onSuccess(savedID)
}
}, [savedID, onSuccess])
@mfunkie
mfunkie / slim-redux.js
Created September 23, 2015 01:39 — forked from gaearon/slim-redux.js
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
if (fn(obj[key])) {
@mfunkie
mfunkie / gist:65e94f42fee3962b1703
Created May 27, 2015 18:44
Relay Talk Transcription by NPM
>> I work on products infrastructure team at
Facebook, and back in January at react day two of my
teammates Dan safer gave us introducing relay, which is
a data stream work. So today in this talk, going to
start with a description of relay for anyone who didn't
see this or who needs an infrastructure, and then I'm
going to dive into specific cards of the relay.
So let's start by thinking about how we at
Facebook were dining inclines a year ago. At that point
we had developed react, and which also buys a pattern
@mfunkie
mfunkie / gist:1efbfa3d570c83defab8
Created January 5, 2015 02:33
I hate myself for this
var items = [1, 2, 3, 4];
switch (true) {
case 2 === items.filter(function(item) { return item > 2; }).length:
console.log('Two items greater than two');
break;
default:
console.log('Not exactly two items greater than two');
}
@mfunkie
mfunkie / exampleUsage.html
Created August 15, 2014 14:48
Template Popover for Angular-UI Bootstrap
<div ng-app="myModule">
<script type="text/ng-template" id="myTemplatePopover.html">
<div ng-controller="PopoverTestCtrl">
<span>Is it not <b style="color:red;">glorious</b> to have <i>html</i> in a popover?</span>
<span>{{ theStuff }}</span>
<button class="btn btn-danger" ng-click="$popover.close()">Try closing here!</span>
</div>
</script>
<button class="btn btn-success" template-popover="myTemplatePopover.html" template-popover-title="I'm a title!" template-popover-placement="bottom">You can have popovers with templates too!</button>