DEV Community

Himani Mehra
Himani Mehra

Posted on

React notes

๐Ÿง  Emmet (Shortcuts for HTML/CSS)

  • What: A shortcut tool in editors like VS Code to write HTML/CSS faster.
  • Why: Saves time by expanding short code into full HTML/CSS.
  • HTML Example:
    ul>li*3
    โžœ Expands to:

  • CSS Example:

    m10

    โžœ Expands to:

    margin: 10px;

Usage: Just type and press Tab.
Library vs Framework

  • Library: ๐Ÿ‘‰ You call the code when needed (You decide when and where to use it.) ๐Ÿ‘‰ You are in control. (e.g., React, Lodash)

Framework:
๐Ÿ‘‰ Framework calls your code.
๐Ÿ‘‰ It controls the flow. (It tells when to run your code โ€” you just โ€œplug inโ€ your logic.)
(e.g., Angular, Next.js)
โœ… Why React is a Library?

  • React only handles the UI (view).
  • You decide how the rest of the app works.
  • It doesnโ€™t force structure or tools.

๐Ÿงฉ Tools You Choose in React:

  • Routing: react-router-dom
  • State: useState, Redux, Zustand, Recoil
  • Data Fetching: fetch, axios, React Query, SWR
  • Forms: Formik, React Hook Form
  • Styling: CSS, SCSS, Tailwind, styled-components
  • Testing: Jest, React Testing Library, Cypress
  • Build Tools: Vite, Webpack, CRA, Next.js

๐ŸŽฏ Summary: React is flexible โ€” itโ€™s a UI library, not a full framework.

๐Ÿง  Soโ€ฆ is Vue a library or a framework?
โžก๏ธ Vue 2: Mostly considered a library (like React) โ€” focuses on the view layer.
โžก๏ธ Vue 3 with ecosystem (Vuex, Vue Router, CLI): Becomes a framework.
React Vue 2 โ€” library ; Vue 3 + Tools Framework-ish ; Angular โ€” Full Framework

๐Ÿง  How does the browser know about document, createElement, etc.?
These come from Browser APIs, not from JavaScript itself.

๐Ÿ”ง How it works:

  1. The browser has a JavaScript engine (like Chromeโ€™s V8).
  2. The browser provides built-in objects like: โ€” window โ€” document โ€” console โ€” localStorage โ€” fetch, etc.
  3. These are called Web APIs โ€” part of the browser environment, not core JavaScript.

๐Ÿ“Œ Example:

const div = document.createElement(โ€œdivโ€);

  • document comes from the DOM API.
  • Itโ€™s not part of core JS โ€” itโ€™s provided by the browser when JS runs in the browser.

โš ๏ธ Note:
If you run JavaScript outside the browser (like in Node.js),
โ†’ You wonโ€™t get document or window โ€” because thereโ€™s no DOM.

โœ… In short:
JS runs inside the browserโ€™s environment,
and the browser gives extra objects (like document) via Browser APIs.

We can inject react library with a bare minimum thing

What is CDN?

๐Ÿง  What is a CDN?

CDN = Content Delivery Network

Itโ€™s a network of distributed servers that deliver files (JS, CSS, images) from the nearest location to the user โ€” making websites faster and more reliable.

๐ŸŒ Real-world example:

When you visit a site using a CDN:

If youโ€™re in India, the content may load from a server in Mumbai
If youโ€™re in the US, it loads from a New York or LA server
This minimizes latency and speeds up loading.

๐Ÿ“ฆ How does it relate to React?

React can be included via CDN links instead of installing via npm.

Example:

<script src=โ€https://unpkg.com/react@18/umd/react.development.js"></script>
<script src=โ€https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>

๐Ÿง  Real-life example of using a CDN:

Letโ€™s say you want to use a **carousel/slider** in your web app.

Instead of downloading the whole library,
you can just include its **CDN link** in your HTML:

Enter fullscreen mode Exit fullscreen mode


html
<! โ€” Slick Carousel CSS โ†’

<! โ€” Slick Carousel JS โ†’

๐Ÿš€ Why use CDN links?

Quick setup โ€” no build tools needed
Great for testing or small apps
No need to install Node.js or npm
โš ๏ธ Not ideal for large/production apps โ†’ Use bundlers like Vite or Webpack for better control.

A CDN serves files faster from nearby servers.

Reactโ€™s CDN version lets you use React by just adding tags โ€” no installation required.</p> <p>๐Ÿง  What is <code>crossorigin</code> in a <code>&lt;script&gt;</code> or <code>&lt;link&gt;</code> tag?<br> The <code>crossorigin</code> attribute tells the browser how to handle <strong>cross-origin requests</strong> โ€” when youโ€™re loading files from a <strong>different domain</strong> (like a CDN).</p> <p>โ€” -</p> <p>๐ŸŒ Example:<br> </p> <div class="highlight"><pre class="highlight html"><code><span class="nt">&lt;script </span><span class="na">src=</span><span class="s">โ€https://unpkg.com/react@18/umd/react.development.js"</span> <span class="na">crossorigin</span> <span class="nt">&gt;&lt;/script&gt;</span> ๐Ÿง  What does *โ€œdifferent domainโ€* mean in `crossorigin`? It means the file (like a script or image) is **NOT hosted on your website**, but coming from **another server**. โ€” - ๐ŸŒ Example: You made a website: ๐Ÿ‘‰ `https://myportfolio.com` Now, you add this: </code></pre></div> <p><br> html</p> <script src=โ€https://unpkg.com/react@18/umd/react.development.js" crossorigin>

๐Ÿ“ฆ That script is from https://unpkg.com โ€” a different domain.

So: myportfolio.com โ‰  unpkg.com โ†’ cross-origin request
Different domainโ€ means the file is loaded from another website/serve

๐Ÿง  Shortest React program (with CDN):

<!DOCTYPE html>
<html>
<head>
<script src=โ€https://unpkg.com/react@18/umd/react.development.js"></script>
<script src=โ€https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
</head>
<body>
<div id=โ€rootโ€></div>
</body>
</html>|
โœ… This runs without any build tools (like npm, Vite, etc.)
โœ… Just open in browser โ€” youโ€™ll see: Hello React

why? Because in console write React and it give React object (like windows object). Also React is a global object

React is just a peice of JS code written by meta developers.

Why 2 files : React and ReactDOM ; because react is not just limited to browsers. Thers is react native for mobile, react 3D as well, react also used in rendering engines.

So react@18 is core library of react
react-dom: the web version of react

Heading using React
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
  <script
    crossorigin
    src="https://unpkg.com/react@18/umd/react.production.min.js"
  ></script>
  <script
    crossorigin
    src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
  ></script>
</html>
<script>
  const heading = React.createElement("h1", {}, "Heading using react"); // it taskes 3 argument - 1.the tag 2.object 3.value
</script>
// h1 comes from the core library of react
now heading is created, now we need to put heading into the root element

  <script>
    const heading = React.createElement("h1", {}, "Heading using react");
    const root = ReactDOM.createRoot(document.getElementById("root")); //this tells react what is the root element inside my app. Also here we used ReactDOM because now we are manipulating DOM
    root.render(heading) // passing react element inside the root
  // So this render method takes react element and injects it into the DOM and modifies it
  </script>
๐Ÿง  Is `createElement` an API?

Yes โ€” `React.createElement()` is a **React API**.

Itโ€™s a method provided by the React library to **create React elements** (UI building blocks) without using JSX.

โ€” -

๐Ÿ”ง Syntax:

Enter fullscreen mode Exit fullscreen mode


js
React.createElement(type, props, children)

if we do:
console.log(heading); // it is plain JS object

// so the react code that meta developers wrote is taking : "h1", {}, "Heading usi

Top comments (0)