Hey folks! Today was one of those sessions where the lightbulb π‘ really went off β I finally understood some confusing but fundamental stuff in React and JavaScript. Here's a breakdown of what I learned (and trust me, it's gold π for beginners and even intermediates who missed the tiny details):
βοΈ 1. Why JSX Needs Double Curly Braces for Styling
"Wait, I'm passing a JavaScript object, so why can't I just wrap it directly with {} like that?"
<button {style={backgroundColor: "red"}}>Red</button> β
But now I know:
Because JSX doesn't allow curly braces around attributes like that. In JSX, attributes must be in the format:
attributeName={jsValue}
<button style={{ backgroundColor: "red" }}>Red</button>
So in incorrect example:
<button {style={...}}> ... </button>
You're trying to put JavaScript inside the HTML tag syntax itself, which breaks JSX rules.
JSX expects props (like style) to be written as:
<tagName propName={jsExpression} />
In our case:
<button style={ { backgroundColor: "red" } }>Red</button>
This is valid because:
- style is the prop name
- { ... } is the JSX way of saying βthe value of this prop is a JavaScript expressionβ
- And that expression happens to be an object π
π 2. Does the Whole Component Re-render on State Change?
I asked:
When I click a button and change color using
setColor("pink")
, does the whole component re-render? Does React repaint everything?
β Answer: Yes, the function re-runs, but React is smart!
- The entire JSX is re-evaluated
- But React only updates the actual DOM parts that changed (thanks to Virtual DOM and diffing)
So your UI stays efficient and smooth π§β¨
π 3. Why Does console.log()
Run Twice in React?
I dropped a console.log("let's see")
inside JSX and freaked out when it printed twice π
π‘ Reason:
React.StrictMode
in development intentionally renders components twice (but ONLY in dev mode) to catch bugs.
β Production will render just once. No worries! All part of the dev experience.
π§ 4. JS Return Rule: Why Code After return
is Ignored
I made this mistake:
return (
<>
<h1>Hi</h1>
</>
);
<App />; // β doesn't work
Lesson learned:
β‘οΈ After return
, nothing else inside the function runs.
So if you want to return <App />
, include it inside the return (...)
block.
π¦ 5. Do I Have to Use the Same Prop Name?
I was passing state like this:
<App count={count} />
And I wondered: Do I have to call it count
in the child?
β Nope!
You can rename it however you want when passing:
<App totalClicks={count} />
Just make sure the name matches in the child:
function App({ totalClicks }) {
return <h2>{totalClicks}</h2>;
}
π Arrays/Objects confusion β Built-in or User-Defined?
Absolute π₯ of a question:
βWhy do we call arrays/objects user-defined if we didnβt define how they work?β
Hereβs the truth:
Object and Array types are built-in to JavaScript β
But when you create a structure with them, like:
const person = { name: "Aman", age: 22 };
const colors = ["red", "green"];
That structure is user-defined β you decide the keys, values, and layout. You didnβt create the engine, but you decided whatβs inside it.
π§ Built-in container, user-defined content.
Primitive types
These are the basic building blocks β they are predefined and immutable (can't be changed directly).
String
Number
Boolean
These are called primitive because:
They hold single values.
They are not objects.
They are copied by value.
So yes, they are predefined by the language.
π‘ TL;DR:
Type | Predefined | User-defined | Stored As |
---|---|---|---|
Primitive | β Yes | β No | By value |
Non-primitive | β (base) + β you define shape | β Yes | By reference |
π§© Topics I Crushed Today:
- JSX attribute syntax
- React re-render logic βοΈ
- StrictMode behavior π
- JavaScript return rules π§
- Prop naming and passing π¨
If you're learning React β bookmark this, share it, and remember:
Even your "smallest doubts" are super valid β clear them early and loud! π
Let me know if you want this as a tweet thread, blog intro, or carousel β we can style it however you like! π¨
Top comments (1)
Again, great stuff! Keep it up!