Skip to content

Instantly share code, notes, and snippets.

View troygoode's full-sized avatar
🤓
Building nerdy stuff.

Troy Goode troygoode

🤓
Building nerdy stuff.
View GitHub Profile
@troygoode
troygoode / use-state-safe.js
Last active July 27, 2021 05:44 — forked from AlpacaGoesCrazy/useSafeState.js
Hook for react state which prevents updates on unmounted components (in TypeScript)
import { useEffect, useRef, useState, Dispatch, SetStateAction } from "react";
/* originally via:
https://gist.github.com/AlpacaGoesCrazy/25e3a15fcd4e57fb8ccd408d488554d7
*/
const useStateSafe = function<T>(initialValue: T): [T, Dispatch<SetStateAction<T>>] {
const isMounted = useRef(false); // useRef to memorize if the component is mounted between renders
const [state, setState] = useState<T>(initialValue);
export default function<TValue>(
value: TValue | null | undefined
): value is TValue {
return value !== null && value !== undefined
}
@troygoode
troygoode / Async.js
Last active May 21, 2021 09:06
type-safe Fetch & Re-Fetch with Hooks & TypeScript
import React from 'react'
interface IState {
isLoading: boolean
error?: Error
}
interface IErrorProps {
error: Error
}
@troygoode
troygoode / example.jsx
Last active January 2, 2019 05:39
I <3 React Hooks
import React, { useState, useMemo } from 'react'
import { useInput } from 'react-hanger' // form binding hooks
import { usePromise } from 'react-hook-utils' // fetch/promise binding hooks
import { getJSON, postJSON } from './fetch-json'
const ExampleForm = ({ userId }) => {
// hooks!
const [user, errorLoadingUser, isLoadingUser] = usePromise(useMemo(
() => getJSON(`/api/users/${userId}`),
[userId] // memo-ize on userId; only re-fetch when the userId prop changes
@troygoode
troygoode / example.jsx
Last active January 2, 2019 05:37
more Hook goodness
import React from 'react'
import { useInput } from 'react-hanger'
const DEFAULT_NAME_VALUE = 'Troy'
const DEFAULT_EMAIL_VALUE = 'troygoode@gmail.com'
const ExampleForm = () => {
const name = useInput(DEFAULT_NAME_VALUE)
const email = useInput(DEFAULT_EMAIL_VALUE)
@troygoode
troygoode / messages-aside.js
Created December 30, 2018 20:27
Hooks are awesome.
import React, { useMemo } from 'react'
import { usePromise } from 'react-hook-utils'
import useMessages from './use-messages'
import Loading fom './loading'
import Oops fom './oops'
const fetchMessages = async () => {
return fetch('/api/messages')
}
@troygoode
troygoode / k8s.config
Last active October 10, 2018 16:58
Avatar Resizer (k8s)
kind: Service
apiVersion: v1
metadata:
name: lanetix-resizer
labels:
app: lanetix-resizer
spec:
type: LoadBalancer
selector:
app: lanetix-resizer
@troygoode
troygoode / dashboard.sh
Created July 30, 2018 16:17
K8s Dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/heapster.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
kubectl apply -f https://gist.githubusercontent.com/troygoode/102596f2ebc4211ccbdaf7c738cea7e5/raw/3d2870329af018efcce27d7da8ed8048af90fbf7/eks-admin-service-account.yaml
kubectl apply -f https://gist.githubusercontent.com/troygoode/102596f2ebc4211ccbdaf7c738cea7e5/raw/3d2870329af018efcce27d7da8ed8048af90fbf7/eks-admin-cluster-role-binding.yaml
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep eks-admin | awk '{print $1}')
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: eks-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
@troygoode
troygoode / markdown-styling.css
Last active June 6, 2018 23:34
Lanetix markdown styling updates
.record-field > span > div {
background-color: #f5f5f5;
padding: 1em;
border-radius: 4px;
margin-top: .5em;
}
.record-field > span > div hr {
margin: 1em 0 !important;
border: 0 !important;