Skip to content

Instantly share code, notes, and snippets.

View anthowave's full-sized avatar
🏠
Working from home

anthowave

🏠
Working from home
View GitHub Profile
@anthowave
anthowave / FormSelect(TS).md
Created July 27, 2020 18:06
A demo project about building a custom FormSelect component using @react-hook-form
@anthowave
anthowave / type.ts
Created August 9, 2020 14:08
TypeScript Challenge - define types with keys or values extracted from object
const COUNTRY_CODES = {
"US": "United of States",
"CA": "Canada",
"GB": "United Kingodm",
};
// type definition
type CountryCode = keyof typeof COUNTRY_CODES; // equivalent to: type CountryCode = "US" | "CA" | "GB";
type CountryName = typeof COUNTRY_CODES[CountryCode]; // equivalent to: type CountryName = "United of States" | "Canada" | "United Kingdom";
@anthowave
anthowave / tools.md
Created August 16, 2020 14:46
useful tools for writing docs.

Package/library docs mostly come as static site.

Here are the list of useful SSGs(static site generator):

React

  • Next.js
  • Gatsby.js
  • React-static

Vue

@anthowave
anthowave / README.md
Created August 22, 2020 08:32 — forked from iansu/README.md
Create React App 4.0 Alpha Testing

Create New App

JavaScript Template

npx create-react-app@next --scripts-version=@next --template=cra-template@next my-js-app

TypeScript Template

npx create-react-app@next --scripts-version=@next --template=typescript@next my-ts-app

@anthowave
anthowave / CountdownTimer.js
Created November 3, 2020 22:24
CountdownTimer react component
import React from 'react'
export default function CountdownTimer({ startSeconds }) {
const [seconds, setSeconds] = React.useState(startSeconds);
const timeoutCallback = React.useCallback(() => {
if(seconds >= 1)
setSeconds(seconds - 1);
}, [seconds]);
@anthowave
anthowave / createCrudHooks.js
Created November 29, 2020 23:45 — forked from tannerlinsley/createCrudHooks.js
A naive, but efficient starter to generate crud hooks for React Query
export default function createCrudHooks({
baseKey,
indexFn,
singleFn,
createFn,
updateFn,
deleteFn,
}) {
const useIndex = (config) => useQuery([baseKey], indexFn, config)
const useSingle = (id, config) =>
@anthowave
anthowave / custom-type-check.ts
Created December 2, 2020 12:50
TS - determine if var is type of custom type
const stringLitArray = <L extends string>(arr: L[]) => arr;
const linkTypes = stringLitArray(['a', 'link']);
export type LinkType = (typeof linkTypes)[number];
const isLinkType = (x: any): x is LinkType => linkTypes.includes(x);
let link = 'a';
if (isLinkType('a')) {
console.log("The current link is of type 'LinkType'");
}
@anthowave
anthowave / useDarkMode.js
Created December 23, 2020 10:57
A custom effect that updates the dark/light mode accordingly when the system setting gets changed
const matchDark = '(prefers-color-scheme: dark)'
function useDarkMode() {
const [isDark, setIsDark] = React.useState(
() => window.matchMedia && window.matchMedia(matchDark).matches
);
React.useEffect(() => {
const matcher = window.matchMedia(matchDark);
const onChange = ({ matches }) => setIsDark(matches);
@anthowave
anthowave / searchDOM.js
Last active December 24, 2020 10:53
6-line javascript code for lazy DFS over the DOM
function* search(node) {
if (!node) return;
yield node;
yield* search(node.firstChild);
yield* search(node.nextSibling);
}
// example usage
for (let node of search(document)) {
if (node.localName === 'title') {