React With Vite
React With Vite
React
React is a popular JavaScript library for building user interfaces,
particularly single-page applications (SPAs).
Vite
Vite is a modern build tool that provides a super-fast development
environment
What is DOM?
The Document Object Model (DOM) is a programming interface for
web documents. It represents the structure of an HTML or XML
document as a tree of objects, where each element (such as <div>,
<p>, <h1>, etc.) is a node in the tree.
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<header>
<h1>Welcome</h1>
</header>
<main>
<p>Hello World!</p>
</main>
<footer>
<span>Copyright 2025</span>
</footer>
</body>
</html>
document
│
├── DOCTYPE: html
└── html
│
├── head
│ └── title
│ └── "My Page"
│
└── body
├── header
│ └── h1
│ └── "Welcome"
│
├── main
│ └── p
│ └── "Hello World!"
│
└── footer
└── span
└── "Copyright 2025"
The DOM Tree
The browser uses the DOM to render a web page and update it
dynamically.
Each HTML element becomes a DOM node.
JavaScript can manipulate the DOM using methods like
document.getElementById() or document.querySelector().
node -v
npm –v
2. Create a Project
Open the terminal and do the following.
cd my-react-app
4. Install Dependencies
npm install
FOLDER STRUCTURE
App.css: This file contains the CSS styles for your main App
component.
index.css: This file contains global CSS styles that apply to your
entire application.
main.jsx: This is the entry point for your React application. It renders
the App component into the DOM.
index.html: The main HTML file that serves as the entry point for your
application. It includes a root div where your React app is mounted.
vite.config.js: This is the configuration file for Vite, where you can
customize the build and development server settings.
REACT BASICS
React Components
1. File Extension
In a Vite + React.js project, React components typically use the .jsx file
extension
// src/components/Greeting.jsx
function Greeting() {
return <h1>Hello, World!</h1>;
}
// src/App.jsx
import Greeting from './components/Greeting';
function App() {
return (
<div>
<Greeting />
</div>
);
}
1. Functional Components
A functional component is a JavaScript function that returns JSX
(JavaScript XML) to describe the UI.
It is simpler and more concise compared to class components.
Example:
function Greeting() {
return (
<>
<h1>Hello, World!</h1>;
</>
)
}
export default Greeting;
JSX
function App() {
return (
<h1>Hello</h1>
<p>World</p>
);
}
export default App;
Example 02: Using a <div> (Works, but Adds Extra DOM Node)
Wrapping elements inside a <div> fixes the issue, but adds an extra
<div> to the DOM.
function App() {
return (
<div>
<h1>Hello</h1>
<p>World</p>
</div>
);
}
export default App;
Example 03: Using <React.Fragment> (No Extra DOM Nodes)
<React.Fragment> <>
Props (Properties)
In React, props (short for "properties") are used to pass data from a
parent component to a child component. Props allow components to
be reusable and dynamic by enabling the parent to send different
values each time the component is used. They are read-only, meaning
a child component cannot modify the props it receives. Instead,
changes must happen in the parent component, which then passes
updated values as props. Props can be any data type, including
strings, numbers, arrays, objects, or even functions. To use props, a
component receives them as an argument and accesses them using
props.propName.
Parent Component
function App() {
return (
<>
<Greeting name="John" age={25} />
</>
)
}
export default App;
Here, pass the name and age props to the Greeting component.
The Greeting component receives name and age as props and
displays them
Child Component
function Greeting(props) {
return (
<>
<h1>Hello, {props.name}!</h1>
<p>Age: {props.age}</p>
</>
);
}
Output:
function App() {
return (
<Modal>
<h2>Delete Account?</h2>
<p>Are you sure you want to delete your account?</p>
<button>Confirm</button>
<button>Cancel</button>
</Modal>
);
}
Child Component
function Modal(props) {
return (
<div style={{
position: "fixed",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
background: "white",
padding: "20px",
boxShadow: "0 0 10px rgba(0,0,0,0.2)",
zIndex: 1000,
}}>
{props.children}
</div>
);
}
Event Handling
In React, event handling is similar to regular JavaScript but follows JSX
syntax, where events like onClick, onChange, and onSubmit are
written in camelCase instead of lowercase (onclick → onClick).
Event handlers are written inside curly braces {}, unlike regular HTML
where they are written as strings. For example, in React, you write
onClick={showAlert} instead of onclick="showAlert()".
Example:
function App() {
function showAlert() {
alert("Button Clicked");
}
return (
<>
<button onClick={showAlert}>Click</button>
</>
)
}
Key hooks like useState allow for direct state management within
functional components, while useEffect handles side effects such as
data fetching or DOM manipulation. Hooks like useContext simplify the
process of accessing context values, and useRef provides a way to
interact with DOM elements or persist values across renders without
triggering re-renders.
function increment() {
setNumber(number + 1);
}
return (
<>
<button onClick={increment}>+</button>
<p>{number}</p>
</>
)
}
Conditional Rendering
Conditional rendering in React allows developers to dynamically
display or hide components and elements based on specific
conditions, enabling more interactive and responsive user interfaces.
Instead of rendering the same elements every time, React can render
specific components, text, or elements based on state, props, or user
interactions. There are several ways to implement conditional
rendering in React.
If-else Statement
Best for complex conditions inside functions.
Message.jsx (Component)
function App() {
return (
<div>
<Message isLoggedIn={true} />
</div>
);
}
Output
Message.jsx (Component)
function App() {
return (
<div>
<Message isLoggedIn={false} />
</div>
);
}
Output
Notification.jsx (Component)
function App() {
return (
<div>
<Notification hasNewMessages={true} />
</div>
);
}
function App() {
return (
<div>
<StatusMessage status="success" />
</div>
);
}
Styling
export in React
default App;
Inline Styles
function App() {
const headingStyle = {
color: "blue",
fontSize: "24px",
backgroundColor: "lightgray",
paragraph: {
fontSize: "20px",
color: "#901000",
},
};
return (
<>
<h1 style={headingStyle}>Hello, React!</h1>
<p style={headingStyle.paragraph}>This is a simple React
application.</p>
</>
);
}
export default App;
Styles are written in external .css files and imported into your components.
style.css
.container {
color:aqua;
background-color: black;
padding: 10px;
}
App.jsx
import "./style.css";
function App() {
return (
<div>
<p className="container"> Hello World </p>
</div>
);
}
For large scale and fast development in React, using CSS frameworks
like Tailwind CSS, Bootstrap can significantly speed up the styling
process.
function App() {
return (
<div>
<img src="/my-image.jpg" alt="My Image" />
</div>
);
}
Example:
function App() {
return (
<div>
<img src={logo} alt="Company Logo"/>
</div>
);
}
The word logo is the variable name you assign to the image file you
imported. It is an arbitrary identifier that you choose yourself when
importing the file. You can use any variable name when importing an
image in React.
Using External Image URLs
Directly use an external URL in the src attribute.
Example:
function App() {
return (
<div>
<img src="https://example.com/my-image.jpg"
alt="External Image" />
</div>
);
}
React Router
React Router enables client-side navigation in single-page
applications (SPAs) by dynamically rendering components based on
the URL.
Basic Routing
function App() {
return (
<>
<BrowserRouter>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />}
/>
</Routes>
</BrowserRouter>
</>
);
}
export default App;
function Navbar() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/contact">Contact</Link>
</nav>
);
}
Nested Routes
<>
<BrowserRouter>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} >
<Route path="dashboard" element={<Dashnoard />} />
</Route>
<Route path="/contact" element={<Contact />} />
</Routes>
</BrowserRouter>
</>
function About() {
return (
<div>
<h1>About Page</h1>