Skip to content

Instantly share code, notes, and snippets.

View itsMapleLeaf's full-sized avatar

itsMapleLeaf

View GitHub Profile
@itsMapleLeaf
itsMapleLeaf / useInstanceValue.ts
Created November 7, 2019 02:16
useInstanceValue - convenience for creating a one-time value for the lifetime of a component
import { useRef } from "react"
const noValue = Symbol()
export default function useInstanceValue<T>(createValue: () => T) {
const ref = useRef<T | typeof noValue>(noValue)
if (ref.current === noValue) {
ref.current = createValue()
}
return ref.current
@itsMapleLeaf
itsMapleLeaf / Portal.tsx
Last active August 16, 2021 02:08
portal
import { ReactNode, useEffect, useRef } from "react"
import { createPortal } from "react-dom"
export function Portal({ children }: { children: ReactNode }) {
const containerRef = useRef<Element>()
if (!containerRef.current) {
containerRef.current = document.createElement("react-portal")
document.body.append(containerRef.current)
}
@itsMapleLeaf
itsMapleLeaf / example.ts
Created September 26, 2019 00:47
TypeScript - filter type keys by value
type Thing = {
a: number
b: number
c: string
d: boolean
}
type Values<O> = O[keyof O]
type KeysWithType<O, T> = Values<{ [K in keyof O]: O[K] extends T ? K : never }>
@itsMapleLeaf
itsMapleLeaf / example.tsx
Last active September 3, 2019 20:22
useWindowIntersection
function Example() {
const elementRef = useRef<HTMLDivElement>(null) // ref for the element you want to watch
const isInsideWindow = useWindowIntersection(elementRef)
return <div ref={elementRef}>
is inside window: {isInWindow}
</div>
}
@itsMapleLeaf
itsMapleLeaf / co.ts
Last active August 29, 2019 15:22
theoretical typed co function
type CoGen<R> = {
next(): IteratorYieldResult<undefined>
next<T>(value: T): IteratorResult<Promise<T>, R>
}
async function co<R>(genfn: () => CoGen<R>): Promise<R> {
const it = genfn()
let result: IteratorResult<unknown, R> = it.next()
while (true) {
if (result.done) {
@itsMapleLeaf
itsMapleLeaf / example.tsx
Created August 13, 2019 12:20
geolocation hook example
function SomeComponent() {
const { location, error } = useGeolocation()
useEffect(() => {
if (error) {
Analytics.trackEvent("LocationService: ", error.message, error.code)
handleLocationError(error)
}
}, [error])
@itsMapleLeaf
itsMapleLeaf / Pixiv Public API.yaml
Created August 5, 2019 02:34 — forked from ZipFile/Pixiv Public API.yaml
Unofficial API specification extracted from Pixiv Android App
swagger: "2.0"
info:
title: "Pixiv Public API"
description: "Unofficial API specification extracted from Pixiv Android App v4.8.2"
version: "1.0"
host: public-api.secure.pixiv.net
schemes:
- https
basePath: /v1
produces:
@itsMapleLeaf
itsMapleLeaf / example.tsx
Last active October 29, 2019 20:38
root store hook example
// in RootStore.ts
export class RootStore {
userStore = new UserStore()
chatStore = new ChatStore()
appNavigationStore = new AppNavigationStore()
}
export const RootStoreContext = React.createContext<RootStore>()
export function useRootStore() {
const missingValue = Symbol()
function createContextWrapper<R, I extends object>(hook: (init: I) => R) {
const Context = React.createContext<R | typeof missingValue>(missingValue)
function Provider(props: I & { children: React.ReactNode }) {
const context = hook(props)
return <Context.Provider value={context}>{props.children}</Context.Provider>
}
class App extends Component {
state = {isLoading:true,
query:'',
focus:'',
places:[]}
getVenues = (location) => {
const endPoint = 'https://api....'
const parameters = {...bla-bla...}
axios.get(endPoint+new URLSearchParams(parameters))