You're asking about a very common React pattern using the useState hook—but there are some non-trivial and deeply useful lessons buried in this simple line:
jsCopiarEditarconst [savedMarkdown, setSavedMarkdown] = useState(initialMarkdown);
Let’s break it down both at surface level and then dig into the deeper lessons for web design and React architecture.
🔹 What this syntax does (basic explanation)
This is array destructuring used to:
Declare a state variable called savedMarkdown
Provide a function setSavedMarkdown that updates the value of savedMarkdown
Initialize the state with initialMarkdown
useState is a React hook that lets functional components store local state (like class components used to with this.state).
Example:
jsCopiarEditaruseState("Hello")
// returns ["Hello", setStateFunction]
⚠️ Non-trivial and useful concepts
Let’s go deeper into what you’re really learning and why it’s powerful in real-world web design:
✅ 1. Destructuring = clearer logic
React returns a 2-element array, and you’re using ES6 destructuring to name them clearly:
savedMarkdown: current value
setSavedMarkdown: function to change it
👉 Lesson: Always name your state and updater semantically. Avoid generic names like value and setValue. Clear naming is key to scalable UIs.
✅ 2. State persists across renders
Each time your component re-renders, React keeps the same savedMarkdown value internally, unless changed by setSavedMarkdown.
👉 Lesson: React is declarative. Your code doesn’t imperatively mutate UI—it declares what UI should look like given the current state.
✅ 3. Functional vs class-based thinking
You're using useState because you're in a functional component, not a class. That means:
No this
Logic is cleaner and more reusable
Easier to compose with other hooks (useEffect, etc.)
👉 Lesson: Modern React favors functional components with hooks. Embrace that mindset early.
✅ 4. Initial state is set once
initialMarkdown is used only on the first render. Changing initialMarkdown later won’t reset the state.
This is often misunderstood.
jsCopiarEditarconst [x, setX] = useState(Math.random());
The Math.random() is called only once, not on every render.
👉 Lesson: If you want the initial value to be computed lazily, use:
jsCopiarEditaruseState(() => computeExpensiveInitialValue());
This avoids unnecessary computation.
✅ 5. UI re-renders on state change
Whenever you call:
jsCopiarEditarsetSavedMarkdown("new markdown");
Your component re-renders, and React shows the UI based on the new savedMarkdown.
👉 Lesson: Think of state as the single source of truth for your UI. Your design updates itself automatically.
✅ 6. Why useState matters in web design
As a web designer learning React:
useState lets you build interactive UI (like toggling tabs, modals, forms)
State is essential for live previews (e.g., showing formatted Markdown as user types)
State links user interaction and visual feedback—a core concept in good UX design
✳️ Summary
This line does more than it seems:
jsCopiarEditarconst [savedMarkdown, setSavedMarkdown] = useState(initialMarkdown);
You're learning:
How to store and manage state
How re-renders are triggered
How to write semantic, readable code
How React hooks empower interactive UI
That initial values are only used once
Would you like to see a small Markdown editor example using this to tie it all together?