Hey there! 👋 As someone with a background in marketing rather than coding, I know firsthand how intimidating React can be. All those technical terms and developer jargon can make your head spin, right? That's why I created this guide - to explain useState() in a way that actually makes sense to normal humans.
If you've ever felt lost in the world of React (I definitely have!), or if technical documentation makes you want to cry, then this guide is for you. Let's break this down into bite-sized, digestible pieces!
*No technical jargon was harmed in the making of this guide* 😉
Imagine you're building with LEGO blocks. React Hooks are like those special LEGO pieces that have specific powers - they help your regular LEGO pieces do cool things. They're basically pre-built features that React gives us to use in our projects.
Think of Hooks like a Swiss Army knife - each tool has a specific purpose:
Remember those magic boards where you could write something, erase it, and write something new? useState is exactly like that for your website! It helps your website remember things and update them when needed.
It looks like this in code:
const [something, setSomething] = useState(startingValue);
Think of it as:
Let me explain this like a cooking recipe:
When you want to change an ingredient amount:
Let's look at some examples that actually make sense:
import React, { useState } from "react";
function WelcomeMessage() {
// Think of this like a greeting card where you can change the name
const [name, setName] = useState("Guest")
return (
<div>
<input
value={name}
onChange = {(e) => setName(e.target.value)}
placeholder="Type your name"
/>
<p>Welcome to my website, {name}! 🎉</p>
</div>
)
}
function LikeButton() {
// Just like counting likes on Instagram
const [likes, setLikes] = useState(0)
return (
<div>
<p>This post has {likes} likes</p>
<button onClick={() => setLikes(likes + 1)}>♥️ Like</button>
</div>
)
}
function DarkModeSwitch() {
// Like a light switch for your website
const [isDark, setIsDark] = useState(false)
return (
<div style={{
background: isDark ? 'black' : 'white',
color: isDark ? 'white' : 'black'
}}>
<button onClick={() => setIsDark(!isDark)}>
Switch to {isDark ? '☀️ Light' : '🌙 Dark'} Mode
</button>
</div>
)
}
// 🚫 Don't do this!
const [user, setUser] = useState({name: 'John'})
user.name = 'Jane' // This is like trying to edit a photocopy instead of the original
// ✅ Do this instead!
setUser({...user, name: 'Jane'}) // This is like making a new copy with the changes
// 🚫 This is like trying to count in your head while someone's talking to you
setCount(count + 1)
setCount(count + 1) // Oops, this won't work right!
// ✅ This is like writing down each step
setCount(previousCount => previousCount + 1)
setCount(previousCount => previousCount + 1) // Much better!
Use useState when you need to:
Don't use useState when:
useState might seem scary at first, but it's really just a way to help your website remember things - like a digital sticky note system! The key is to:
Remember, everyone starts somewhere, and if you're coming from a non-technical background like me, it's totally normal to take time to understand these concepts. Just keep practicing, and it'll start to make sense!
*Stay tuned for more guides where I break down other React concepts into normal human language!* 💪
Questions? Feel free to reach out! And remember - if I can understand this stuff coming from a marketing background, you definitely can too! 🚀