interview questions Part -1
interview questions Part -1
1. What is React?
o React is a JavaScript library used for building user interfaces, particularly
single-page applications. It allows developers to create reusable UI
components that manage their own state and render efficiently when data
changes.
Advanced Questions
What is JSX?
JSX stands for JavaScript XML. It is a syntax extension for JavaScript, commonly
used with React. JSX allows you to write HTML elements within JavaScript code.
React uses it to describe the UI structure.
JSX is not natively understood by browsers. When you write JSX, it is transformed
into JavaScript by tools like Babel. For example, the JSX element <h1>Hello,
World!</h1> is compiled to React.createElement('h1', null, 'Hello,
World!').
No, JSX is not mandatory in React. You can use plain JavaScript with
React.createElement() to create elements, but JSX is more concise and readable,
which is why it's commonly used.
JSX allows you to write HTML-like syntax directly in JavaScript, which improves
readability and productivity. It also helps avoid errors since the JavaScript logic and
markup are closely related. Moreover, JSX makes it easier to visualize the structure of
a component.
Can you pass variables or expressions inside JSX?
Yes, you can pass JavaScript expressions inside JSX by wrapping them in curly
braces {}. For example: <h1>{name}</h1>, where name is a JavaScript variable.
In JSX, class is replaced with className to avoid conflicts with the JavaScript
class keyword.
Yes, you can use conditional rendering inside JSX with ternary operators or logical &&
operator. For example:
jsx
Copy code
<div style={{ color: 'red', fontSize: '20px' }}>Hello</div>
return (
<>
<h1>Hello</h1>
<p>Welcome to React</p>
</>
);
Components Based Question
24. What is the difference between useEffect with an empty dependency array and
componentDidMount?
o useEffect with an empty dependency array ([]) behaves similarly to
componentDidMount in class components. It runs only once after the first render,
making it ideal for performing setup tasks such as data fetching.
25. What are Render Props in React?
o Render Props is a pattern for sharing code between React components using a prop
that is a function. The function returns JSX that will be rendered. It allows
components to share their logic without explicitly inheriting from one another.
jsx
Copy code
<SomeComponent render={(data) => <div>{data}</div>} />