React is a JavaScript library for building user interfaces, primarily for single-page applications, where you can manage and interact with the page without refreshing it. React enables developers to build web applications that can update and render efficiently in response to data changes.
Components are the building blocks of a React application. They let you split the UI into independent, reusable pieces. 2. Props: Props are read-only components that allow you to pass arguments or data from a parent component to a child component. 3. State: The state is a data structure that starts with a default value when a component mounts. It is mutable and used for managing data in a component. 4. JSX: JSX (JavaScript XML) is a syntax extension for JavaScript, recommended by React. It looks similar to XML or HTML and describes what the UI should look like. 5. Lifecycle Methods: Lifecycle methods are hooks that allow the execution of code at different times in a component’s life.
To get started with React, you can use Create React App (CRA), a tool that sets up a new React project with a good default configuration. In a terminal, run:
npx create-react-app my-app
cd my-app
npm start
Below is an example of a simple React component:
import React from 'react';
class HelloWorld extends React.Component {
render() {
return <h1>Hello, World!</h1>;
}
}
export default HelloWorld;
Components can be either functional or class components. Functional components are simpler and are used for presenting static views, and class components are used when you need to manage local state or lifecycle. Here’s a functional component example:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
And a class component example:
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Props (short for "properties") are a way of passing data from parent to child components. Here’s how to pass and receive props:
// Parent Component
<Welcome name="Sara" />
// Child Component
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
State is used to store local component data that can change over time. Below is an example of how to use state in a class component:
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
handleIncrement = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<p>{this.state.count}</p>
<button onClick={this.handleIncrement}>Increment</button>
</div>
);
}
}
For functional components, React introduced Hooks in version 16.8. The `useState` Hook is a function that lets you add state to functional components. Here’s how you can use the `useState` Hook to manage state in a functional component:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const handleIncrement = () => {
setCount(count + 1);
};
return (
<div>
<p>{count}</p>
<button onClick={handleIncrement}>Increment</button>
</div>
);
}
Events in React components are written in camelCase, and with JSX you pass a function as the event handler.
<button onClick={handleIncrement}>Increment</button>
Here, `handleIncrement` is a function defined in your component, which will be called when the button is clicked.
Lifecycle methods are special methods in the class components that allow you to run JavaScript code at particular times in the component’s life. For instance, `componentDidMount` is called once the component is inserted into the DOM, and `componentWillUnmount` is called right before the component is removed from the DOM.
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.
function Greeting(props) {
if (props.isLoggedIn) {
return <UserGreeting />;
}
return <GuestGreeting />;
}
To render multiple components, you can build them in an array using the JavaScript `map()` function. Keys should be given to the elements inside the array to give the elements a stable identity:
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<li key={number.toString()}>
{number}
</li>
);
In React, the `input`, `textarea`, and `select` behave a little bit differently from other DOM elements, as they keep their own state. To make their states controllable by React, you can set the `value` attribute to the corresponding state and handle the `onChange` event:
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<button type="submit">Submit</button>
</form>
);
}
}
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here