Skip to content

Instantly share code, notes, and snippets.

View kwoncharles's full-sized avatar
🐎

Charles Kwoncheol Shin kwoncharles

🐎
View GitHub Profile
@kwoncharles
kwoncharles / MyComponent.ts
Last active March 1, 2020 09:31
usePrevious hook example
function MyComponent() {
const [alphabet, set] = useState('A');
const prevAlphabet = usePrevious(alphabet);
useEffect(() => {
if (alphabet === 'C' && prevAlphabet === 'B') {
doSomething();
}
}, [alphabet]);
// ..
import { useEffect, useRef } from 'react';
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
import { useState, useCallback, useRef } from 'react';
import { removeNonNumeric, parseNumWithMaxValue } from '@/utils/StringUtils';
interface Options {
initialValue?: string;
maxValue?: number;
minValue?: number;
maxLength?: number;
minLength?: number;
autoFix?: boolean;
import React from 'react';
import Helmet from 'react-helmet';
function My() {
return (
<>
<Helmet>
<title>My page</title>
<meta property="description" content="discover yourself" />
#[derive(Debug)]
struct Currency {
currency_symbol: char,
exchange_rate: i32
}
impl Currency {
fn to_won(&self, amount: i32) -> i32 {
self.exchange_rate * amount
}
impl Currency {
fn to_won(&mut self, amount: i32) -> i32 {
self.exchange_rate = 1200;
self.exchange_rate * amount
}
}
fn main() {
let dollar: Currency = Currency {
currency_symbol: '$',
fn main() {
let reference_to_nothing = dangle();
}
fn dangle() -> &String {
let s = String::from("hello");
&s
}
fn main() {
let mut s = String::from("hello");
change(&mut s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}
fn main() {
let s = String::from("hello");
change(&s);
}
fn change(some_string: &String) {
some_string.push_str(", world");
}
fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len);
}
fn calculate_length(s: &String) -> usize {
s.len()