What are Props in React?
Props (Properties) are used in React to pass data from a parent component to a child component. They allow components to be reusable by receiving different values from their parent.
Props are read-only, meaning the child component should not directly modify the props it receives.
Examplefunction Student(props) {
return <h2>Student Name: {props.name}</h2>;
}
function App() {
return <Student name="Rahul" />;
}
Here, name="Rahul" is a prop passed from the App (parent) component to the Student (child) component. The child accesses it using props.name.
What are Components in React? Explain in Detail.
Components are the building blocks of a React application. A component is a reusable piece of UI that defines how a particular part of a web page should appear and behave.
Components divide a large application into small, independent, and reusable parts, making the application easier to develop and maintain.
Types of Components
React mainly has two types of components:
A Functional Component is a JavaScript function that returns JSX. It is the commonly used approach for creating components in modern React.
Examplefunction Welcome() {
return <h1>Welcome to React</h1>;
}
export default Welcome;
Here, Welcome is a functional component that returns an <h1> element. Functional components can use Hooks such as useState() and useEffect() to manage state and perform other React operations.
A Class Component is a JavaScript class that extends React.Component and contains a render() method that returns JSX.
import React from "react";
class Welcome extends React.Component {
render() {
return <h1>Welcome to React</h1>;
}
}
export default Welcome;
Here, Welcome is a class component and the render() method returns the UI.
Advantages of Components
Explain Form Handling in React
A form in React is used to collect input from users, such as name, email, message, etc. Forms are created using JSX elements such as <form>, <input>, <button>, <textarea>, and <select>.
React handles form data differently from plain HTML by generally using Controlled Components.
Controlled Components
A controlled component is a form element whose value is controlled by React state. The input's value comes from state, and whenever the user changes the input, the state is updated using onChange.
import { useState } from "react";
function SimpleForm() {
const [name, setName] = useState("");
return (
<form>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<p>You typed: {name}</p>
</form>
);
}
Here, value={name} makes the input display the current state value, onChange runs whenever the user types something, and setName() updates the state. React therefore always knows the current value of the input.
Form Submission
The onSubmit event is used to handle form submission.
function SimpleForm() {
const [name, setName] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log("Submitted:", name);
};
return (
<form onSubmit={handleSubmit}>
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
<button type="submit">Submit</button>
</form>
);
}
The important statement is e.preventDefault();. Normally, submitting an HTML form reloads the page. e.preventDefault() prevents the default page reload, allowing React to handle the submission using JavaScript.
Handling Multiple Fields
Multiple form fields can be stored in a single state object:
const [formData, setFormData] = useState({
name: "",
email: ""
});
const handleChange = (e) => {
setFormData({
...formData,
[e.target.name]: e.target.value
});
};
Here, e.target.name identifies which input was changed, e.target.value contains the entered value, and ...formData preserves the existing field values.
Other Form Components
React also supports controlled textarea and select elements using the same value and onChange approach.
Explain Hooks in React with Examples. Describe the Rules to Follow When Using a Hook.
Hooks are special functions in React that allow functional components to use React features such as state, lifecycle-related behavior, context, DOM access, and performance optimization. All React Hooks start with the word use. They were introduced in React 16.8, allowing functional components to use features that previously required class components.
Common React Hooks
Example of useState()
useState() is used to store and update data inside a functional component. When the state changes, React re-renders the component and updates the UI.
import { useState } from "react";
function App() {
const [count, setCount] = useState(0);
return (
<>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</>
);
}
Here, count stores the current value and setCount() updates it.
Example of useEffect()
useEffect() is used to perform side effects after a component is rendered, such as API calls, fetching data, timers, event listeners, or updating the browser title.
import { useEffect } from "react";
function App() {
useEffect(() => {
alert("Welcome Students");
}, []);
return <h1>Hello</h1>;
}
Rules to Follow When Using Hooks
Hooks should not be called inside loops, conditions, nested functions, or if statements. They should be called at the top level of the functional component.
Hooks should be called from a React functional component or from a custom Hook, not from an ordinary JavaScript function.
For example, avoid:
if (isLoggedIn) {
useState(0); // ❌
}
Instead, call the Hook first and put the condition around the logic that uses its value.
React Hooks start with use, such as useState, useEffect, and useContext. Custom Hooks should also start with use, for example useStudentData.