Props are used to pass data from parent component to child component.
function Welcome({ name, role }) {
return (
<div>
<h2>Welcome {name}</h2>
<p>Your role is: {role}</p>
</div>
);
}
export default Welcome;import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import Welcome from './components/welcome'
function App() {
const [count, setCount] = useState(0)
return (
<>
<div>
<Welcome name="Shree" role="Software Developer"></Welcome>
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.jsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
}
export default App
Here, App is the parent component and Welcome is the child component.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
{/* count → state variable */}
{/* setCount → function to update state */}
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
<button onClick={() => setCount(count - 1)}>
Decrease
</button>
</div>
);
}
export default Counter;
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import Welcome from './components/welcome'
import Counter from './components/Counter'
function App() {
const [count, setCount] = useState(0)
return (
<>
<div>
<Welcome name="Shree" role="Software Developer"></Welcome>
<hr />
<h1>State & Events Example</h1>
<Counter></Counter>
<hr />
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.jsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
}
export default App
| State | Props |
|---|---|
| Stores data that can change within a component. | Used to pass data from parent to child component. |
| Managed inside the component. | Received from parent component. |
| Can be updated using useState hook. | Read-only and cannot be modified by child. |
| Used for handling user interactions. | Used for sharing static or dynamic data. |
| Changes in state re-render the component. | Props change only when parent re-renders. |
import "./CssDemo.css";
function CssDemo()
{
const internalStyle={
color:"orange",
backgroundColor: "white",
padding: "10px",
marginBottom: "10px"
};
return(
<div>
<h1>CSS Types in React</h1>
{/* External CSS */}
<div className="external-style">
This is External CSS
</div>
{/* Internal CSS */}
<div style={internalStyle}>
This is Internal CSS
</div>
{/* Inline CSS */}
<div style={{
color: "black",
backgroundColor: "yellow",
padding: "10px"
}}>
This is Inline CSS
</div>
</div>
);
}
export default CssDemo;
.external-style {
color: white;
background-color: #0d6efd;
padding: 10px;
margin-bottom: 10px;
}
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import Welcome from './components/welcome'
import Counter from './components/Counter'
import CssDemo from './components/CssDemo'
function App() {
const [count, setCount] = useState(0)
return (
<>
<div>
<Welcome name="Shree" role="Software Developer"></Welcome>
<hr />
<h1>State & Events Example</h1>
<Counter></Counter>
<hr />
<CssDemo></CssDemo>
<hr />
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.jsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
}
export default App
| Key Point | Description |
|---|---|
| className | Use className instead of class to apply CSS classes in JSX. |
| External CSS | Best for large and reusable styles; keep CSS near related components. |
| Inline CSS | Styles are written as JavaScript objects inside JSX. |
| Internal CSS | Defined as JavaScript objects inside the component for reuse. |
| Assets Folder | Recommended for global styles and static files, not component-specific CSS. |
npm install bootstrap
import 'bootstrap/dist/css/bootstrap.min.css';function Header() {
return (
<nav className="navbar navbar-dark bg-dark">
<div className="container">
<span className="navbar-brand">My React App</span>
</div>
</nav>
);
}
export default Header;
function Footer() {
return (
<footer className="bg-light text-center py-3 mt-4">
<p className="mb-0">© 2026 React App</p>
</footer>
);
}
export default Footer;
function Home() {
return (
<div className="container mt-4">
<h2 className="text-primary">Home Page</h2>
<p>Welcome to the home page of our React application.</p>
</div>
);
}
export default Home;
function About() {
return (
<div className="container mt-4">
<h2 className="text-success">About Page</h2>
<p>This application is built using React and Bootstrap.</p>
</div>
);
}
export default About;
import Header from "./components/Header";
import Footer from "./components/Footer";
import Home from "./components/Home";
import About from "./components/About";
function App() {
return (
<>
<Header />
<Home />
<About />
<Footer />
</>
);
}
export default App;