Skip to content

Instantly share code, notes, and snippets.

View bmingles's full-sized avatar

Brian Ingles bmingles

View GitHub Profile
@bmingles
bmingles / MyComponent.tsx
Last active January 9, 2022 23:44
Example of using Mobx + React
import React from 'react'
import { observer } from 'mobx-react-lite'
const MyComponent: React.FC = () => {
// I would typically get this from context API with custom hook instead of local useMemo
// e.g. const { name, age, loadAge, loadName } = useSomeService()
const { age, name, loadAge, loadName } = React.useMemo(() => new SomeService(), [])
React.useEffect(() => {
loadAge()
@bmingles
bmingles / Startup script
Last active November 28, 2021 21:35
Azure App Service Config
# Configuration -> General Settings -> Startup Command
pm2 serve /home/site/wwwroot/ --no-daemon --spa
@bmingles
bmingles / namedContext.tsx
Last active September 19, 2021 04:19
React Context Utils and Observable Hooks
import React from 'react'
/**
* Create a React Context that requires a value be provided explicitly (aka. no
* default value). Returns the Context Provider + a useContext hook that
* encapsulates access to the Context.
*/
export function createNamedContext<TValue>(name: string) {
const Context = React.createContext<TValue | null>(null)
@bmingles
bmingles / SomeStory.tsx
Created August 12, 2021 21:44
Storybook hide actions panel and disable controls
parameters: {
controls: {
disable: true,
},
options: { showPanel: false },
}
@bmingles
bmingles / typescript.json
Created August 3, 2021 18:10
vscode snippets
{
// Place your snippets for typescript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
@bmingles
bmingles / useSetProp.hook.ts
Created November 20, 2020 16:00
Custom React hook for setting properties
import React from 'react'
/**
* Custom hook to set single properties on state objects.
*
* e.g.
* const [state, setState] = useState({ name: 'John Doe' })
* const setProp = useSetProp(setState)
*
* const setName = setProp('name')
@bmingles
bmingles / TypeScript Eslint Yarn Fixes
Last active October 12, 2020 20:20
package.json
"resolutions": {
"**/@typescript-eslint/eslint-plugin": "^4.2.0",
"**/@typescript-eslint/parser": "^4.2.0",
"**/eslint-plugin-react-hooks": "^4.1.2"
}
@bmingles
bmingles / ValidatedInput.tsx
Created July 24, 2020 16:46
React + TypeScript ValidatedInput component
import React from 'react'
import { isError, useDependentState } from '../util'
import Bulma from '../bulma'
export type ValidatedValue<
TValue,
TRequired extends boolean
> = TRequired extends true ? TValue : TValue | null
export interface ValidatedInputProps<TValue, TRequired extends boolean> {
@bmingles
bmingles / switchExp.ts
Last active May 25, 2020 04:14
Tagged Union Switch Expression
type Tagged<T extends string> = {
type: T
}
export function switchExp<
T extends Tagged<string>,
M extends { [P in T['type']]: T extends Tagged<P> ? (t: T) => any : never }
>(value: T, map: M): ReturnType<M[keyof M]> {
return map[value.type as keyof M](value)
}
@bmingles
bmingles / router.ts
Created May 21, 2020 01:25
Strong typed routing
export interface Spec<K extends string, T> {
ctr: (raw: string) => T,
key: K,
match: RegExp
}
type Data<T, D> = {
type: T
} & { [P in keyof D]: D[P] };