MERN Stack Interview Questions (Answered) To Beat Your Next Tech Interview
MERN Q/A for interview.
Que 1: How does React work?
React works by allowing you to create UI components that automatically update when data changes. It uses a Virtual DOM to efficiently update the real DOM, encouraging a modular, component-based approach to building web interfaces. Data flows unidirectionally from parent to child components, and JSX simplifies the creation of UI elements within JavaScript.
Que 2: What are React Hooks?
React Hooks are functions that allow you to use state and other React features in functional components. They were introduced in React 16.8 as a way to add state and lifecycle features to functional components, which were previously limited to class components. Hooks make it easier to reuse logic between components and manage component state without the need for class components. Some commonly used React Hooks include:
useState
: This Hook allows functional components to manage local component state. You can define and update state variables usinguseState
.
For example:const [count, setCount] = useState(0);
useEffect
: It enables functional components to perform side effects, such as data fetching, DOM manipulation, or subscribing to external events. It’s similar to lifecycle methods in class components.
For example:useEffect(() => { // Your side effect code here }, [dependencies]);
useContext
: This Hook allows functional components to access the context of a parent component. It’s useful for sharing data and functions with multiple nested components without the need for prop drilling.useRef
:useRef
creates mutable object references that can be used to access and manipulate the DOM or store mutable values across renders. It’s often used to interact with DOM elements directly.useReducer
:useReducer
is an alternative touseState
for managing complex state logic. It’s typically used when the state logic becomes more involved, such as managing state transitions based on actions, similar to Redux.- Custom Hooks: Developers can create their own custom hooks to encapsulate and reuse logic across multiple components. Custom Hooks are named with the “use” prefix and can be shared across the application.
React Hooks provide a more straightforward and consistent way to manage state and side effects in functional components, making it easier to write clean and reusable code. They also contribute to the overall simplification of React code and help developers avoid some of the complexities associated with class components.
Que 3: What are props in React?
Props are inputs to a React component. They are single values or objects containing a set of values that are passed to React Components on creation using a naming convention similar to HTML-tag attributes. i.e, They are data passed down from a parent component to a child component.
The primary purpose of props in React is to provide following component functionality:
- Pass custom data to your React component.
- Trigger
state
changes. - Use via
this.props.reactProp
inside component’srender()
method.
For example, let us create an element with reactProp property,
<Element reactProp = "1" />
This reactProp
(or whatever you came up with) name then becomes a property attached to React’s native props object which originally already exists on all components created using React library.
props.reactProp;