Skip to content

Instantly share code, notes, and snippets.

View tjinlag's full-sized avatar

Tín Nguyễn tjinlag

View GitHub Profile
@tjinlag
tjinlag / react-memo.js
Created July 8, 2021 04:37
how to use React.memo()
const MyComponent = (props) => {
/* render using props */
}
const areEqual = (prevProps, nextProps) => {
/*
return true if passing nextProps to render would return
the same result as passing prevProps to render,
otherwise return false
*/
@tjinlag
tjinlag / react-memo.js
Last active July 8, 2021 05:25
React.memo() with function in props
const MemoizedComponent = React.memo(MyComponent);
// In this case, the MemoizedComponent will re-render when the App re-render.
// Because when the app re-render, the function handleClick will have the new reference.
const App = () => {
const handleClick = () => {
// logic when click ...
}
return (
@tjinlag
tjinlag / App.js
Created July 8, 2021 08:09
Debounce input
const { useState } from "react";
const App = () => {
const [value, setValue] = useState("");
const handleChange = (text) => {
console.log('input value changed: ', text);
setValue(text);
}
return (
@tjinlag
tjinlag / useToastContent.js
Created July 8, 2021 08:15
custom hook: useToastContent - using for toast a message, and then automatically hide it (after a timeout, eg: 2s)
export const useToastContent = (timeout = 2e3) => {
const [content, setContent] = useState('');
const timer = useRef();
useEffect(() => {
return () => {
if (timer && timer.current) {
clearTimeout(timer.current);
}
}
}, []);
@tjinlag
tjinlag / useDisableScroll.js
Last active July 8, 2021 09:41
React Custom Hook: don't allow scroll window
export const useDisableScroll = () => {
useEffect(() => {
const { body } = document;
const bodyStyle = { ...body.style };
body.style.height = '100%';
body.style.overflow = 'hidden';
body.style.position = 'relative';
return () => {
@tjinlag
tjinlag / useTitle.js
Last active July 8, 2021 09:40
React custom hook: set document title when enter a page
const DEFAULT_TITLE = "Your default title";
const useTitle = (title) => {
useEffect(() => {
document.title = title || DEFAULT_TITLE;
}, [title]);
};
export default useTitle;
@tjinlag
tjinlag / useNavigate.js
Created July 8, 2021 09:39
custom hook to navigate to another url in React App
const isFullUrl = (url = "") => url.match(/^\w+:\/\//);
const useNavigate = () => {
const history = useHistory();
const navigateTo = (url: string, { replace = false } = {} ) => {
if (isFullUrl(url)) {
window.location.href = url;
return;
}
@tjinlag
tjinlag / useOnlineStatus.js
Created July 8, 2021 09:49
React Custom Hook: get network status - online or offline
import { useState, useEffect } from "react";
const useOnlineStatus = () => {
const [online, setOnline] = useState(true);
useEffect(() => {
const goOnline = () => setOnline(true);
const goOffline = () => setOnline(false);
window.addEventListener('online', goOnline);
@tjinlag
tjinlag / useCountDown.js
Created July 8, 2021 09:55
React Custom Hook: count down time to zero (seconds)
const useCountDown = (timeout = 59) => {
const [expiredTime] = useState(Math.round(Date.now() / 1e3) + timeout);
const [remainTime, setRemainTime] = useState(expiredTime - Math.round(Date.now() / 1e3));
useEffect(() => {
const timer = setTimeout(() => {
const diff = expiredTime - Math.round(Date.now() / 1e3);
if (diff < 0) {
if (remainTime !== 0) {
setRemainTime(0);
@tjinlag
tjinlag / useTimers.js
Created July 8, 2021 10:05
React Custom Hook: manage multi timers,and destroys it when unmount
const useTimers = () => {
const timers = useRef<any[]>([]);
useEffect(() => {
return () => {
// eslint-disable-next-line
timers.current.map((timer) => {
if (timer) {
clearTimeout(timer);
}
return null;