Day-3 of Learning React.js

PUBLISHED: MAY 2, 2026β€’2 MIN READ

Day-3 of Learning React.jsπŸ“˜ Step-by-Step Explanation1. Importsimport React, { useState } from "react"; React β†’ allows you to write JSX (HTML-like code inside J

Abhijeet Singh Rajput Profile Photo
Abhijeet Singh RajputAuthor
infernape
#392
infernape

Day-3 of Learning React.js

πŸ“˜ Step-by-Step Explanation

1. Imports

javascript
import React, { useState } from "react";
  • React β†’ allows you to write JSX (HTML-like code inside JavaScript).
  • useState β†’ a React hook that lets you store and update data inside a function component.

2. Function Component

javascript
function App() {
  • App is a function component.
  • Components are like building blocks of your UI.
  • This one is the main component of your app.

3. useState Hook

javascript
const [theme, setTheme] = useState("dark");
  • theme β†’ state variable that stores the current theme ("dark" by default).
  • setTheme β†’ function used to change the theme.
  • useState("dark") β†’ initializes the state with "dark".

πŸ‘‰ Think of it like:

  • theme = box holding the current theme.
  • setTheme = tool to replace what’s inside the box.

4. Toggle Function

javascript
const toggleTheme = () => {
  const newTheme = (theme === "dark") ? "light" : "dark";
  setTheme(newTheme);
};
  • This function switches between "dark" and "light".
  • (condition ? true : false) β†’ called ternary operator.
    • If theme === "dark", then newTheme = "light".
    • Otherwise, newTheme = "dark".
  • setTheme(newTheme) β†’ updates the state with the new theme.

5. JSX (UI Part)

javascript
return (
  

Lorem ipsum...

);
  • <div className={"main " + theme}>
    • Adds a CSS class dynamically.
    • Example: if theme = "dark", class becomes "main dark".
    • If theme = "light", class becomes "main light".
  • <button onClick={toggleTheme}>
    • Button that runs toggleTheme when clicked.
    • Inside button β†’ {theme} shows current theme text (dark or light).
  • <p>...</p> β†’ just a sample paragraph.

6. Export

javascript
export default App;
  • export default β†’ sends out the whole component so other files can use it.
  • If you used just export, you could export multiple things individually.

7. Your Comments

javascript
// export default ka matalb entire file
// export ka matalb indivusual
// we wrap the js code in {} curly brases
  • βœ… Correct!
  • export default β†’ one main thing from file.
  • export β†’ many individual things.
  • {} curly braces β†’ used in JSX to run JavaScript code inside HTML-like structure.

πŸ“ Key Notes in Simple Words

  • React β†’ library for building UI with components.
  • Component β†’ function that returns UI.
  • useState β†’ lets you store and change data.
  • Ternary Operator β†’ short way to write if...else.
  • Dynamic className β†’ changes CSS class based on state.
  • Controlled UI β†’ React decides what to show based on state.
  • export default β†’ export one main thing.
  • {} curly braces β†’ run JavaScript inside JSX.