this
keyword.π Example of a Simple Functional Component:
function Greeting() {
return <h2>Hello, Welcome to React!</h2>;
}
useState
, useEffect
, etc.).A functional component is simply a function that returns JSX.
π Example: Hello.js
Functional Component
function Hello() {
return <h1>Hello, World!</h1>;
}
export default Hello;
App.js
Now, use the Hello
component inside App.js
.
π Example: App.js
import Hello from "./Hello"; // Importing the component
function App() {
return (
<div>
<Hello /> {/* Using the Hello component */}
</div>
);
}
export default App;
Props (short for properties) allow you to pass data to functional components.
π Example: Functional Component with Props
function Welcome(props) {
return <h2>Welcome, {props.name}!</h2>;
}
Using Props in App.js
import Welcome from "./Welcome";
function App() {
return (
<div>
<Welcome name="Zia" /> {/* Passing data as props */}
</div>
);
}
export default App;
useState
(Using Hooks)Functional components can use state with React Hooks like useState
.
π Example: Counter Component Using useState