Hey there, Fellow Coder! đ
Want to learn React but getting a headache reading the super stiff official documentation? Relax, youâre in the right place. Here, weâre going to talk about React like weâre just chilling with friends over coffee. No complicated jargon that makes you frown.
Todayâs menu is React Basic Concepts. Weâre going to dissect its three main pillars: JSX, Props, and State.
Ready? Letâs dive in! đ
What is React Anyway?
Imagine youâre playing with LEGOs. If you want to build a LEGO castle, you donât mold one giant castle straight from the factory, right? You build it from small bricks. There are red bricks, blue bricks, windows, doors, roofs, etc.
Well, React is like playing LEGO for websites.
We donât create one massive index.html file that goes on forever. We break it down into small components (the LEGO bricks).
- A button is a component.
- The header is a component.
- A profile card is a component.
Then, these components are assembled into a complete page. Neat, reusable, and easy to manage. Cool, right? đ
1. JSX: HTML Lost in JavaScript
When you first see React code, you might be shocked: âWait, why is there HTML inside JavaScript? Thatâs an error!â
Example:
const Introduction = () => {
return <h1>Hello, I'm Jules!</h1>;
};
Relax, thatâs not an error. Thatâs called JSX (JavaScript XML). React allows us to write UI structure (similar to HTML) directly inside JavaScript logic.
Why? For convenience! You donât need to jump back and forth between HTML and JS files to manage the view. Everything is in one place. Later, React (with the help of tools like Babel) will translate that into regular JavaScript that the browser understands.
Just remember a few rules:
- Since itâs JavaScript, keywords are slightly different.
classin HTML becomesclassNamein JSX (becauseclassis already taken in JS). - Close all tags!
<br>must become<br />. Donât be lazy! đ
2. Props: Passing Notes
Okay, we have a component. But a purely static component is boring. What if we want to create a âBusiness Cardâ component with different content for each person?
This is where Props (short for Properties) come in. Think of Props like function arguments or parameters. Or simply: like passing a note to a friend.
// BusinessCard component receives a "note" (props)
const BusinessCard = (props) => {
return (
<div className="card">
<h3>Hello, my name is {props.name}</h3>
<p>Job: {props.job}</p>
</div>
);
};
// How to use it:
<BusinessCard name="John" job="Code Carpenter" />
<BusinessCard name="Jane" job="Cool Designer" />
See? One BusinessCard component can be reused many times with different data.
Important: Props are âgivenâ. The component receiving props cannot change them. It can only read them.
3. State: The Componentâs Memory
If Props are data from outside (given by someone), State is data from inside (owned by the component). State is like the componentâs memory. It can change over time.
Classic example: a Counter. The number starts at 0. If a button is clicked, the number increases to 1, 2, 3, etc. This changing number must be stored in State.
In modern React (using Hooks), we use a magic spell called useState.
import { useState } from "react";
const Counter = () => {
// We create a state 'count', initial value is 0
// 'setCount' is the button/function to change the value
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click Me!</button>
</div>
);
};
Simple Analogy:
- Props: Like the text on your t-shirt. Others can see it, but you canât change it (unless you change shirts).
- State: Like your mood. Can be happy, sad, hungry. Itâs inside you and can change depending on the situation (e.g., fed -> happy).
Closing
So, how was it? React isnât that scary, is it? đ Basically, itâs just:
- Build Components (assemble LEGOs).
- Use JSX (write HTML in JS).
- Pass Data with Props.
- Store Changing Data with State.
In the next article, weâll discuss something even more exciting: Hooks and how to fetch data from the internet (API Fetching). Stay tuned!
Donât forget to code today! đ»â