Answer: React is a JavaScript library for building user interfaces, particularly single-page applications (SPAs). It allows developers to create reusable UI components, manage the state efficiently, and handle updates in a predictable way using a virtual DOM.
Â
Answer: JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript. React components are often written using JSX, which is then compiled into React.createElement calls.
Â
Answer: The Virtual DOM is a lightweight representation of the real DOM. When a component’s state changes, React updates the Virtual DOM first, then compares it with the previous Virtual DOM using a process called “reconciliation.” It then updates the real DOM only with the necessary changes, making it faster and more efficient.
Â
Answer: Props (short for “properties”) are used to pass data from a parent component to a child component in React. They are read-only and help components remain reusable by making them configurable with different values.
Â
Answer: State is a built-in object in React components that holds data that may change over time. It determines the component’s behavior and how it renders. Unlike props, state is mutable and managed within the component itself.
Â
Answer:
Answer: A controlled component is an input element whose value is controlled by the state of a React component. The value of the input is updated by calling setState()
.
class Form extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
}
handleChange(event) {
this.setState({ value: event.target.value });
}
render() {
return <input type="text" value={this.state.value} onChange={(e) => this.handleChange(e)} />;
}
}
Answer:
React.Component
. They can hold state, use lifecycle methods, and handle more complex logic.Answer: Hooks are functions that allow you to use state and other React features in functional components. Commonly used hooks include:
useState()
: Adds state to functional components.useEffect()
: Performs side effects in functional components.useContext()
: Consumes values from a React Context.useRef()
: Accesses a DOM element or stores mutable data.Answer: useEffect()
is a hook used to perform side effects in functional components. It is similar to lifecycle methods (componentDidMount
, componentDidUpdate
, etc.) in class components. The hook takes a function that will run after the component renders.
Example:
useEffect(() => {
console.log('Component has mounted or updated');
}, [dependencies]);
Answer: Redux is a state management library often used with React to manage and centralize the application’s state. It uses a single immutable state tree and actions to describe state changes. A reducer function processes these actions to update the state.
Â
Answer: Lifecycle methods are special methods in class components that allow you to hook into different stages of the component’s lifecycle (e.g., mounting, updating, and unmounting). Some key lifecycle methods are:
componentDidMount()
: Called after the component is mounted.shouldComponentUpdate()
: Determines if a component should re-render.componentDidUpdate()
: Called after a component updates.componentWillUnmount()
: Called just before the component is unmounted.componentDidMount()
and useEffect()
?Answer:
componentDidMount()
is a class component lifecycle method that is invoked once, immediately after the component is mounted to the DOM.useEffect()
is a hook in functional components that can be used to perform side effects. It runs after every render, but you can control when it runs by passing a dependency array.Answer: The Context API is a way to pass data through the component tree without having to pass props down manually at every level. It is useful for sharing global data like themes, user authentication, etc.
Example:
const MyContext = React.createContext();
function App() {
return (
<MyContext.Provider value="Hello World">
<Child />
</MyContext.Provider>
);
}
function Child() {
const value = useContext(MyContext);
return <div>{value}</div>;
}
Answer: Keys are unique identifiers given to elements in lists when rendering them in React. They help React identify which items have changed, are added, or are removed, which improves the performance of updates. Keys should be stable and unique to ensure efficient re-renders.
Example:
const list = items.map((item) => <li key={item.id}>{item.name}</li>);
Answer: Lazy loading is a technique where components are loaded only when they are needed (i.e., when they are about to be rendered). React provides React.lazy()
and Suspense
for this purpose. This helps reduce the initial load time of a React application by splitting the bundle into smaller pieces.
Example:
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}
Answer: React Fragments allow you to group a list of children without adding extra nodes to the DOM. It is useful when you want to return multiple elements from a component but don’t want to introduce unnecessary divs.
Example:
return ( <>
<h1>Title</h1>
<p>Some text</p>
</>
);
useState
and useReducer
?Answer:
useState
is used for managing simple state, typically for single values like numbers, strings, or booleans.useReducer
is used for more complex state logic that involves multiple sub-values or more complicated state transitions, often resembling Redux.Answer: React events are normalized across different browsers, meaning they work consistently. React uses camelCase for event names (e.g., onClick
, onChange
) and passes an event object as the argument to the event handler. React event handlers are written as functions, not strings.
Example:
function handleClick() {
alert('Button clicked!');
}
<button onClick={handleClick}>Click Me</button>
Â
useRef
hook used for in React?Answer: useRef
is used to persist values across renders without causing re-renders. It can be used to access a DOM element directly or to store a mutable value that does not trigger a re-render when it changes.
Example:
const inputRef = useRef();
const focusInput = () => {
inputRef.current.focus();
};
return
( <>
<input ref={inputRef} />
<button onClick={focusInput}>Focus Input</button>
</>
);
Â