Skip to content

Instantly share code, notes, and snippets.

View JaysonChiang's full-sized avatar

Jayson JaysonChiang

View GitHub Profile
import { useState } from 'react';
const TodoList: React.FC = () => {
const [todo, setTodo] = useState('');
const [todos, setTodos] = useState<string[]>([]);
const onClick = () => {
setTodo('');
setTodos([...todos, todo]);
};
@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]);
};
@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>
interface ChildProps {
name: string;
onClick: () => void;
}
const Child = ({ name, onClick }: ChildProps) => {
return (
<div>
{name}
<button onClick={onClick}>Click</button>
abstract class Bike {
abstract brandname: string;
ride() {
return `I ride ${this.brandname}`;
}
}
class Giant extends Bike {
brandname = 'Giant';
}
// public method
function Bike() {}
Bike.prototype.ride = function () {
return "I ride " + this.brand;
};
// static method
function BikeMaker() {}
BikeMaker.factory = function (brandname) {
function thousandComma(number) {
var num = number.toString();
var pattern = /(-?\d+)(\d{3})/;
while (pattern.test(num)) {
num = num.replace(pattern, "$1,$2");
}
return num;
}