This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { useState } from "react"; | |
const App = () => { | |
const [value, setValue] = useState(""); | |
const handleChange = (text) => { | |
console.log('input value changed: ', text); | |
setValue(text); | |
} | |
return ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const useToastContent = (timeout = 2e3) => { | |
const [content, setContent] = useState(''); | |
const timer = useRef(); | |
useEffect(() => { | |
return () => { | |
if (timer && timer.current) { | |
clearTimeout(timer.current); | |
} | |
} | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const useDisableScroll = () => { | |
useEffect(() => { | |
const { body } = document; | |
const bodyStyle = { ...body.style }; | |
body.style.height = '100%'; | |
body.style.overflow = 'hidden'; | |
body.style.position = 'relative'; | |
return () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DEFAULT_TITLE = "Your default title"; | |
const useTitle = (title) => { | |
useEffect(() => { | |
document.title = title || DEFAULT_TITLE; | |
}, [title]); | |
}; | |
export default useTitle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useTimers = () => { | |
const timers = useRef<any[]>([]); | |
useEffect(() => { | |
return () => { | |
// eslint-disable-next-line | |
timers.current.map((timer) => { | |
if (timer) { | |
clearTimeout(timer); | |
} | |
return null; |
OlderNewer