React Js
React Js
React Js
Introduction
React.js is a popular JavaScript library for building user interfaces, particularly single-
page applications where data changes over time. Developed by Facebook, React
enables developers to create large web applications that can update and render
efficiently in response to data changes.
Core Concepts
1. Components
Components are the building blocks of a React application. They are JavaScript
functions or classes that optionally accept inputs (called “props”) and return React
elements that describe how a section of the UI should appear.
jsx
Copy code
function Welcome(props) {
jsx
Copy code
JSX is a syntax extension for JavaScript that looks similar to HTML. It is used with
React to describe what the UI should look like.
Example:
jsx
Copy code
function App() {
return (
<div>
<h1>Welcome to React!</h1>
</div>
);
3. Props
Props (short for properties) are read-only attributes used to pass data from parent
components to child components.
Example:
jsx
Copy code
function Greeting(props) {
function App() {
return (
<div>
</div>
);
4. State
State is used to manage data that changes over time within a component. Unlike
props, state is mutable and can be changed by the component itself.
Example:
jsx
Copy code
return (
<div>
</div>
);
5. Lifecycle Methods
Lifecycle methods are hooks in class components that allow you to run code at
specific points in a component’s lifecycle, such as when it mounts or updates.
Example:
jsx
Copy code
componentDidMount() {
}
componentDidUpdate() {
render() {
Setting Up React
The easiest way to start a new React project is by using Create React App, a
command-line tool that sets up a new React project with a sensible default
configuration.
Steps:
bash
Copy code
bash
Copy code
cd my-app
bash
Copy code
npm start
This will open your new React application in the default web browser at
http://localhost:3000.
2. Directory Structure
java
Copy code
my-app/
├── node_modules/
├── public/
│ ├── index.html
│ └── favicon.ico
├── src/
│ ├── App.js
│ ├── index.js
│ ├── components/
│ └── styles/
├── .gitignore
├── package.json
└── README.md
• src/: Contains the JavaScript and CSS files. This is where you will write your
React components.
Advanced Topics
1. Handling Forms
Forms in React are controlled components, meaning that form data is handled by the
state of the component.
Example:
jsx
Copy code
function Form() {
};
e.preventDefault();
};
return (
<form onSubmit={handleSubmit}>
<label>
Name:
</label>
<br />
<label>
Email:
</label>
<br />
<button type="submit">Submit</button>
</form>
);
2. React Hooks
React Hooks are functions that let you use state and other React features without
writing a class. Common hooks include useState, useEffect, and useContext.
Example of useEffect:
jsx
Copy code
function Timer() {
useEffect(() => {
3. Context API
The Context API is used to pass data through the component tree without having to
pass props down manually at every level.
Example:
jsx
Copy code
// Create Context
// Provider Component
return (
{children}
</ThemeContext.Provider>
);
}
// Consumer Component
function ThemedComponent() {
return (
<div>
Toggle Theme
</button>
</div>
);
// App Component
function App() {
return (
<ThemeProvider>
<ThemedComponent />
</ThemeProvider>
);
Example:
bash
Copy code
jsx
Copy code
function Home() {
return <h2>Home</h2>;
function About() {
return <h2>About</h2>;
function App() {
return (
<Router>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
</nav>
<Switch>
</Switch>
</Router>
);
Best Practices
1. Component Composition
Compose components rather than inheriting them. Use small, reusable components
to build up larger components.
2. State Management
For complex state management, consider using libraries like Redux or the React
Context API.
3. Performance Optimization
4. Testing
Test components using libraries like Jest and React Testing Library to ensure your
components work as expected.
5. Code Quality
Follow consistent coding conventions and use linters (e.g., ESLint) to maintain code
quality.