This file contains hidden or 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 MultipleEffects = () => { | |
// 🍟 | |
useEffect(() => { | |
const clicked = () => console.log('window clicked') | |
window.addEventListener('click', clicked) | |
return () => { | |
window.removeEventListener('click', clicked) | |
} | |
}, []) |
This file contains hidden or 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 EffectCleanup = () => { | |
useEffect(() => { | |
const clicked = () => console.log('window clicked') | |
window.addEventListener('click', clicked) | |
// return a clean-up function | |
return () => { | |
window.removeEventListener('click', clicked) | |
} | |
}, []) |
This file contains hidden or 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 BasicEffect = () => { | |
const [age, setAge] = useState(0) | |
const handleClick = () => setAge(age + 1) | |
useEffect(() => { | |
document.title = 'You are ' + age + ' years old!' | |
}) | |
return <div> | |
<p> Look at the title of the current tab in your browser </p> |
This file contains hidden or 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 CounterFnSetState = () => { | |
const [count, setCount] = useState(0); | |
return ( | |
<> | |
<p>Count value is: {count}</p> | |
<button onClick={() => setCount(0)}>Reset</button> | |
<button | |
onClick={() => setCount(prevCount => prevCount + 1)}> | |
Plus (+) | |
</button> |
This file contains hidden or 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 StateFromFn = () => { | |
const [token] = useState(() => { | |
let token = window.localStorage.getItem("my-token"); | |
return token || "default#-token#" | |
}) | |
return <div>Token is {token}</div> | |
} |
This file contains hidden or 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 StateObject = () => { | |
const [state, setState] = useState({ age: 19, siblingsNum: 4 }) | |
const handleClick = val => | |
setState({ | |
...state, | |
[val]: state[val] + 1 | |
}) | |
const { age, siblingsNum } = state | |
return ( |
This file contains hidden or 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 MultipleStateVars = () => { | |
const [age, setAge] = useState(19) | |
const [siblingsNum, setSiblingsNum] = | |
useState(10) | |
const handleAge = () => setAge(age + 1) | |
const handleSiblingsNum = () => | |
setSiblingsNum(siblingsNum + 1) | |
This file contains hidden or 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 UpdateStateVar = () => { | |
const [age, setAge] = useState(19) | |
const handleClick = () => setAge(age + 1) | |
return ( | |
<div> | |
Today I am {age} Years of Age | |
<div> | |
<button onClick={handleClick}>Get older! </button> | |
</div> |
This file contains hidden or 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 DeclareStateVar = () => { | |
const [count] = useState(100) | |
return <div> State variable is {count}</div> | |
} |
This file contains hidden or 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
// 🐢 merge (setState) vs replace (useState) | |
// assume initial state is {name: "Ohans"} | |
setState({age: "unknown"}) | |
// new state object will be | |
// {name: "Ohans", age: "unknown"} | |
useState({age: "unknown"}) | |
// new state object will be | |
// {age: "unknown"} - initial object is replaced |