What is React.js and why is it used?
- Answer: React.js is a JavaScript library for building user interfaces. It allows developers to create reusable UI components and manage the state of an application efficiently. React is used for building single-page applications where the user interface needs to be dynamic and interactive.
What is JSX in React?
- Answer: JSX (JavaScript XML) is a syntax extension for JavaScript recommended by React. It looks similar to XML/HTML, but it gets transpiled to JavaScript. JSX makes it easier to write and understand the structure of React components.
// Example JSX const element = <h1>Hello, React!</h1>;Explain the difference between state and props in React.
- Answer: Props (short for properties) are used to pass data from a parent component to a child component, while state is used to manage the internal state of a component. Props are immutable, and changes in props trigger re-rendering of the component. State is mutable and can be updated using
setState
, triggering a re-render.
- Answer: Props (short for properties) are used to pass data from a parent component to a child component, while state is used to manage the internal state of a component. Props are immutable, and changes in props trigger re-rendering of the component. State is mutable and can be updated using
What is the significance of
key
prop in React lists?- Answer: The
key
prop is used to help React identify which items have changed, been added, or been removed in a list. It should be a unique identifier for each element in the list and helps in optimizing the rendering performance of the component - // Example usage of key prop in a list const listItems = data.map(item => ( <li key={item.id}>{item.name}</li> ));
What is the purpose of React hooks?
- Answer: React hooks are functions that allow functional components to use state and lifecycle features that were previously only available in class components. Examples include
useState
for managing state anduseEffect
for handling side effects.
- Answer: React hooks are functions that allow functional components to use state and lifecycle features that were previously only available in class components. Examples include
- // Example of useState hook const [count, setCount] = useState(0);
Explain the concept of virtual DOM in React.
- Answer: The virtual DOM is a lightweight copy of the actual DOM maintained by React. When the state of a component changes, React first updates the virtual DOM and then compares it with the real DOM to identify the minimal number of changes required. This process improves performance by reducing direct manipulation of the actual DOM.
How does React handle forms?
- Answer: React handles forms by using controlled components. A controlled component is a form element whose value is controlled by React through its state. When the user inputs data, React state is updated, and the input value is controlled by React, allowing for dynamic form behavior.
- // Example of a controlled component const [inputValue, setInputValue] = useState(''); const handleChange = (e) => { setInputValue(e.target.value); }; <input type="text" value={inputValue} onChange={handleChange} />
Explain the concept of lifting state up in React.
- Answer: Lifting state up is a pattern in React where the state is moved to a common ancestor component that needs to share state among its descendants. This allows multiple child components to share the same state and ensures that the state is kept in sync across the components.
Explain the purpose of the
useEffect
hook in React.- Answer: The
useEffect
hook is used to perform side effects in functional components. It can be used for tasks like data fetching, subscriptions, manual DOM manipulations, and more. It takes a function as its first argument and is executed after the component renders.
useEffect(() => { // Side effect code console.log('Component has mounted or updated'); // Cleanup function (optional) return () => { console.log('Component will unmount'); }; }, [/* dependency array */]);- Answer: The
What is the purpose of the
useState
hook in React?- Answer: The
useState
hook allows functional components to have state. It returns an array with two elements: the current state value and a function that lets you update it. The initial state is passed as an argument.
const [count, setCount] = useState(0);- Answer: The
What is React Router?
- Answer: React Router is a library for handling navigation and routing in React applications. It enables the creation of single-page applications with navigation and dynamic content loading without a full page refresh.
- // Example of React Router usage import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const App = () => ( <Router> <div> <nav> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> {/* ... */} </ul> </nav> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> {/* ... */} </div> </Router> );
These questions cover a range of fundamental React concepts. Depending on the level of the interview,