DEV Community

Sowntharya
Sowntharya

Posted on Edited on

ReactJS and Its Core Concepts

What is ReactJS?
ReactJS is a JavaScript library used to build fast and interactive user interfaces (UI), especially for web applications.

  • Developed by Facebook (Meta).
  • Used to build Single Page Applications (SPA).
  • Uses reusable components.
  • Updates only the necessary parts of the UI instead of reloading the entire page.

To create ReactJS Project

npm create vite@latest 
Enter fullscreen mode Exit fullscreen mode

What is JSX (JavaScript XML)?
JSX stands for JavaScript XML. It allows us to write HTML-like syntax inside JavaScript.

function App() {
  return (
    <div>
      <h1>Hello React!</h1>
      <p>Welcome to ReactJS</p>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

JSX makes UI development easier because we can write HTML-like code directly inside JavaScript.

Rules of JSX

  • Always return a single parent element.
  • Use className instead of class.
  • Close all tags, including self-closing tags.
  • Use {} to write JavaScript expressions inside JSX.
  • Use camelCase for event names, such as onClick.
  • Use map() and unique key values when rendering lists.
function App() {
  const name = "React";

  return (
    <div className="container">
      <h1>Hello {name}</h1>
      <input type="text" />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Component-Based Architecture

  • React follows a component-based architecture.
  • A component is a small, reusable part of a user interface.

For example, a website can be divided into:
Header Component
Navbar Component
Sidebar Component
Main Content Component
Footer Component

Advantages:

  • Code reusability.
  • Easy maintenance.
  • Better code organization.
  • Easy testing and debugging.

How Does React Overcome Page Refresh?
In traditional web applications, navigating between pages often causes the browser to reload the entire page.

React uses the Virtual DOM and client-side rendering to update only the required UI without refreshing the entire browser page.

For multiple pages, React Router can be used.

import { BrowserRouter, Routes, Route } from "react-router-dom";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<h1>Home Page</h1>} />
        <Route path="/about" element={<h1>About Page</h1>} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

What is a Functional Component?
A functional component is a JavaScript function that returns JSX.

Example:

function Welcome() {
  return <h1>Welcome to React!</h1>;
}

export default Welcome;
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Simple to write.
  • Easy to reuse.
  • Supports React Hooks.
  • Easier to manage component logic.

What is Re-rendering in React?
Re-rendering means updating a component's UI when its state or props change.

For example:

import { useState } from "react";
function Counter(){
        console.log("component ReRendered....");
    // let cout = 0
    const [count, setCount] = useState(0);

    function increment(){
        // count++
        // console.log(count)
        setCount(count + 1);

    }
}

export default Counter;
Enter fullscreen mode Exit fullscreen mode

The state value changes.
React re-renders the component.
The updated count appears on the screen.

What are React Hooks?
Hooks are special functions that allow functional components to use React features such as state and lifecycle-related behavior.

Example: useState

const [name, setName] = useState("John");
Enter fullscreen mode Exit fullscreen mode

Example: useEffect

useEffect(() => {
  console.log("Component mounted");
}, []);
Enter fullscreen mode Exit fullscreen mode

Hook

  • useState => Manages component state
  • useEffect => Handles side effects
  • useContext => Shares data between components
  • useRef => Stores references or mutable values
  • useMemo => Caches calculated values
  • useCallback => Caches function references

Top comments (0)