- import { PureComponent } from 'react'
+ import { memo } from 'react'
- class Benny extends PureComponent {
- render () {
+ const Benny = memo(() => {
return <Consumer>
{position => }
+ import { PureComponent } from 'react'
- class Benny extends Component {
+ class Benny extends PureComponent {
render () {
return <Consumer>
{position => <svg />}
</Consumer>
}
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 fetchData = () => { | |
const stringifyData = data => JSON.stringify(data, null, 2) | |
const initialData = stringifyData({ data: null }) | |
const loadingData = stringifyData({ data: 'loading...' }) | |
const [data, setData] = useState(initialData) | |
const [gender, setGender] = useState('female') | |
const [loading, setLoading] = useState(false) | |
useEffect( |
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
// 🐢 setState (object merge) vs useState (object replace) | |
// assume initial state is {name: "Ohans"} | |
setState({ age: 'unknown' }) | |
// new state object will be | |
// {name: "Ohans", age: "unknown"} | |
useStateUpdater({ age: 'unknown' }) | |
// new state object will be | |
// {age: "unknown"} - initial object is replaced |
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
function TimerWithRefID() { | |
const setIntervalRef = useRef(); | |
useEffect(() => { | |
const intervalID = setInterval(() => { | |
// something to be done every 100ms | |
}, 100); | |
// this is where the interval ID is saved in the ref object | |
setIntervalRef.current = intervalID; |
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 HoldStringVal = () => { | |
const textAreaEl = useRef(null); | |
const stringVal = useRef("This is a string saved via the ref object --- ") | |
const handleBtnClick = () => { | |
textAreaEl.current.value = | |
stringVal.current + "The is the story of your life. You are an human being, and you're on a website about React Hooks"; | |
textAreaEl.current.focus(); | |
}; | |
return ( | |
<section style={{ textAlign: "center" }}> |
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 AccessDOM = () => { | |
const textAreaEl = useRef(null); | |
const handleBtnClick = () => { | |
textAreaEl.current.value = | |
"The is the story of your life. You are an human being, and you're on a website about React Hooks"; | |
textAreaEl.current.focus(); | |
}; | |
return ( | |
<section style={{ textAlign: "center" }}> | |
<div> |
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 App = () => { | |
const [age, setAge] = useState(99) | |
const handleClick = () => setAge(age + 1) | |
const someValue = useMemo(() => ({ value: "someValue" })) | |
const doSomething = () => { | |
return someValue | |
} | |
return ( | |
<div> |
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 App = () => { | |
const [age, setAge] = useState(99) | |
const handleClick = () => setAge(age + 1) | |
const someValue = { value: "someValue" } | |
const doSomething = () => { | |
return someValue | |
} | |
return ( | |
<div> |
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 App = () => { | |
const [age, setAge] = useState(99) | |
const handleClick = () => setAge(age + 1) | |
const someValue = "someValue" | |
return ( | |
<div> | |
<Age age={age} handleClick={handleClick} /> | |
<Instructions doSomething={useCallback(() => { | |
return someValue |
NewerOlder