Skip to content

Instantly share code, notes, and snippets.

View srph's full-sized avatar
🛠️
Building @Stride-Labs Frontend

Kier Borromeo srph

🛠️
Building @Stride-Labs Frontend
View GitHub Profile
@srph
srph / useBufferState.ts
Last active August 1, 2019 06:49
React Hooks: Show something momentarily.
import { useState, useRef } from 'react'
import useUpdateEffect from 'react-use/lib/useUpdateEffect'
interface Props {
timeout: number
}
type Payload = [boolean, () => void]
function useBufferState(props: Props): Payload {
@srph
srph / readme.md
Last active July 29, 2019 14:51
React: Access updated props inside custom event listeners

Use-case

This is particularly useful if you have to attach to a document event (in my case, a keyboard event). Unless you re-attach the event whenever anything changes, you will have stale props. Now, we don't want that, do we?

In my case, I had a hook which accepted this argument:

interface Props {
  isDisabled: () => boolean
  onToggleChat: () => void
 onToggleMute: () => void
@srph
srph / WindowVhSetter.tsx
Created June 20, 2019 10:04
React: 100vh css variable workaround for mobile (https://css-tricks.com/the-trick-to-viewport-units-on-mobile/)
import { useEffect } from 'react'
import useWindowSize from 'react-use/lib/useWindowSize'
/**
* @NOTE Don't mount this twice; only on the root node.
*/
function WindowVhSetter() {
const {height} = useWindowSize()
@srph
srph / index.tsx
Last active June 12, 2019 00:08
React: Component to maintain an aspect ratio for an image (i.e., forget the hack)
import './style.css'
import * as React from 'react'
export type Props = React.ImgHTMLAttributes<HTMLImageElement> & {
ratio: number
}
function ImageAspectRatio(props: Props) {
const { ratio, ...imgProps } = props
@srph
srph / getStandardFormattedDateTime.ts
Created June 7, 2019 03:24
JS: mysql-formatted string with date-fns
import { format } from 'date-fns'
/**
* Gives you a mysql standard formatted datetime
* e.g., 2018-08-08 23:00:00
*/
function getStandardFormattedDateTime(date: Date = new Date()) {
return format(date, 'YYYY-MM-DD HH-mm-ss')
}
@srph
srph / index.ts
Created June 6, 2019 08:56
JS: Group chat messages based on criteria (initial code for Care.tv)
interface AppPartyLog {
id: number
party_id: number
type: 'activity' | 'message'
activity?: {
id: string
text: string
user: AppUser
}
message?: {
@srph
srph / index.tsx
Created June 4, 2019 22:48
React: Force route to remount (e.g., refetch data) when params change
/**
* Little trick to force our component to remount entirely if the user
* creates a tracker all while viewing another tracker.
*/
function WrappedDashboardTracker(props: Props) {
return (
<DashboardTracker key={props.match.params.trackerId} {...props} />
)
}
@srph
srph / PeriodicNow.ts
Created June 2, 2019 09:55
React: Get the current date periodically
import * as React from 'react'
interface Props {
interval: number
children: (now: Date) => React.ReactNode
}
interface State {
now: Date
}
@srph
srph / compose-refs.ts
Created May 25, 2019 21:30
React: Function to assign multiple refs to an element (compose refs)
function composeRefs<T>(refs: React.Ref<T>[]): React.Ref<T> {
return function(component: T) {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(component)
} else if (ref && 'current' in ref) {
ref.current = component
}
})
}
@srph
srph / index.ts
Created May 21, 2019 12:39
JS: Get remaining time from seconds
interface RemainingTimeValue {
hours: number
minutes: number
seconds: number
}
/**
* Get remaining time from seconds
*
* Used by `toReadableTime` and `distanceInWordsAbbreivated`