π What is Closure in JavaScript? ? Explained with Simple Examples

React JS has revolutionized the way we build modern web applications. If you’re new to React or want a refresher on creating functional components, this post will guide you through it step by step—with a live example included!
A functional component is a JavaScript function that returns a React element (JSX). It’s the simplest way to create components in React.
import React from 'react';
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
export default Greeting;
Now, use this component in your main app file:
import React from 'react';
import ReactDOM from 'react-dom';
import Greeting from './Greeting';
ReactDOM.render(
<Greeting name="Ankur" />,
document.getElementById('root')
);
Hello, Ankur!
This is a dynamic and reusable component. You can change the name prop to anything you want!
const Greeting = ({ name }) => <h1>Hello, {name}!</h1>;
Both styles are valid. Choose what suits your coding style.
Use Create React App to set up your project:
npx create-react-app my-react-app
cd my-react-app
npm start
Add your component files in the src/
folder.
Use the App.js
or index.js
to render them.
Functional components are the heart of modern React. With hooks and clean syntax, they are perfect for beginners and pros alike. Try experimenting with props, JSX, and styles to create something amazing!
useState
, useEffect
, and other hooks
Comments
Post a Comment