This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function SomeComponent() { | |
| const { location, error } = useGeolocation() | |
| useEffect(() => { | |
| if (error) { | |
| Analytics.trackEvent("LocationService: ", error.message, error.code) | |
| handleLocationError(error) | |
| } | |
| }, [error]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // in RootStore.ts | |
| export class RootStore { | |
| userStore = new UserStore() | |
| chatStore = new ChatStore() | |
| appNavigationStore = new AppNavigationStore() | |
| } | |
| export const RootStoreContext = React.createContext<RootStore>() | |
| export function useRootStore() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |