An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
June 28, 2021 06:12 am GMT
Original Link: https://dev.to/thenerdydev/react-hooks-demystifying-the-usestate-hook-in-under-10-minutes-examples-video-29ab
React Hooks : Demystifying the useState hook in under 10 minutes [examples video]
Hey everyone ,
In this article, let us understand about the useState React Hook in under 10 minutes.
Understanding useState
I already have an article that covers the useState hook in great detail. Check this out :
React Hooks Part 1 - Understanding useState and useEffect Hooks
The Nerdy Dev May 9 6 min read
#react #javascript #100daysofcode #codenewbie
Example 1: Playground
CODE :
import { useState } from 'react'; const LearningState = props => { const [bgColor, setBgColor] = useState('red'); const turnGreen = () => setBgColor('green'); const reset = () => setBgColor('red'); return ( <div> <div style={{backgroundColor: bgColor }}> {bgColor === 'green' ? <p>Color is now green</p>: <p>Red</p>} </div> <button onClick={turnGreen}>Turn Green</button> {bgColor === 'green' && <button onClick={reset}>Reset</button>} </div> )}export default LearningState;
Example 2: Counter Example, understanding functional form of useState and batching
CODE :
import { useState } from "react"; const Counter = (props) => { const [counter, setCounter] = useState(0); console.log('runs...'); const incrementCounter = () => { // setCounter(counter + 1); // setCounter(counter + 1); for(let i=0;i<10;i++) { setCounter(prevCounter => prevCounter + 1); } } const decrementCounter = () => { setCounter(previousCounter => previousCounter - 1); setCounter(previousCounter => previousCounter - 1); setCounter(previousCounter => previousCounter - 1); } return ( <div> <h1>Counter : {counter} </h1> <button onClick={incrementCounter}>Increment Counter</button> <button onClick={decrementCounter}>Decrement Counter</button> </div> )}export default Counter;
Video
So this was it for this one.
If you are looking to learn Web Development, I have curated a FREE course for you on my YouTube Channel, check the below article :
(2021) - Web Developer Full Course : HTML, CSS, JavaScript, Node.js and MongoDB
The Nerdy Dev Apr 28 2 min read
#html #css #node #javascript
Spare 2 Hours ? If so, utilize them by creating these 10 JavaScript Projects in under 2 Hours
Follow me on Twitter : https://twitter.com/The_Nerdy_Dev
Check out my YouTube Channel : https://youtube.com/thenerdydev
Original Link: https://dev.to/thenerdydev/react-hooks-demystifying-the-usestate-hook-in-under-10-minutes-examples-video-29ab
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To