Skip to content

Instantly share code, notes, and snippets.

View JaysonChiang's full-sized avatar

Jayson JaysonChiang

View GitHub Profile
function thousandComma(number) {
var num = number.toString();
var pattern = /(-?\d+)(\d{3})/;
while (pattern.test(num)) {
num = num.replace(pattern, "$1,$2");
}
return num;
}
// public method
function Bike() {}
Bike.prototype.ride = function () {
return "I ride " + this.brand;
};
// static method
function BikeMaker() {}
BikeMaker.factory = function (brandname) {
abstract class Bike {
abstract brandname: string;
ride() {
return `I ride ${this.brandname}`;
}
}
class Giant extends Bike {
brandname = 'Giant';
}
interface ChildProps {
name: string;
onClick: () => void;
}
const Child = ({ name, onClick }: ChildProps) => {
return (
<div>
{name}
<button onClick={onClick}>Click</button>
@JaysonChiang
JaysonChiang / Child.tsx
Created February 26, 2021 14:21
TypeScript React Component with React.FC
interface ChildProps {
name: string;
onClick: () => void;
}
const Child: React.FC<ChildProps> = ({ name, onClick }) => {
return (
<div>
{name}
<button onClick={onClick}>Click</button>
@JaysonChiang
JaysonChiang / TodoList.jsx
Last active February 26, 2021 15:18
State with React Component
import { useState } from 'react';
const TodoList = () => {
const [todo, setTodo] = useState('');
const [todos, setTodos] = useState([]);
const onClick = () => {
setTodo('');
setTodos([...todos, todo]);
};
import { useState } from 'react';
const TodoList: React.FC = () => {
const [todo, setTodo] = useState('');
const [todos, setTodos] = useState<string[]>([]);
const onClick = () => {
setTodo('');
setTodos([...todos, todo]);
};
const EventComponent = () => {
const onChange = (event) => {
console.log(event);
};
return (
<div>
<input onChange={onChange} />
</div>
);
const EventComponent: React.FC = () => {
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
console.log(event.target.value);
};
const onDragStart = (event: React.DragEvent<HTMLDivElement>) => {
console.log(event);
};
return (
@JaysonChiang
JaysonChiang / Search.jsx
Last active February 27, 2021 10:18
Simple React Refs Sample
import { useState, useRef, useEffect } from 'react';
const Search = () => {
const inputRef = useRef(null);
const [name, setName] = useState('');
useEffect(() => {
inputRef.current.focus();
}, []);