-
Notifications
You must be signed in to change notification settings - Fork 0
Interview Question (React) ‐ Part 1
Anurag Mishra edited this page Jun 7, 2024
·
2 revisions
- React follows a component-based architecture where UIs are built using reusable components.
- It employs a unidirectional data flow, where data flows from parent to child components.
- React uses a virtual DOM to efficiently update the actual DOM, reducing the number of manipulations needed.
- Components can have their own state, and when the state changes, React automatically re-renders the components.
- React components have lifecycle methods that allow you to perform actions at different stages of a component's existence. These methods include:
componentDidMount
componentDidUpdate
componentWillUnmount
- They help in managing component initialization, updates, and cleanup.
-
Basic Setup:
- Installing dependencies like
react
andreact-dom
- Setting up a build system using tools like Webpack or Create React App
- Creating components
- Installing dependencies like
-
Advanced Configurations:
- Setting up routing
- State management with Redux
- Styling with CSS preprocessors or CSS-in-JS libraries
- Optimizing performance
-
Key Concepts:
- Single immutable state tree
- Actions that describe state changes
- Reducers that specify how actions transform the state
- Store that holds the state
-
Middleware:
- Intercepts dispatched actions, allowing you to perform asynchronous tasks, logging, or other side effects before they reach the reducers.
- Arrow Functions
- Classes
- Template Literals
- Destructuring Assignment
- Spread/Rest Operators
- let and const for block-scoped variables
- These features enhance readability, maintainability, and expressiveness of JavaScript code.
- API calls can be made using:
-
fetch
API - Axios
- Libraries like
react-query
orswr
-
- Typically, you make API calls within lifecycle methods like
componentDidMount
or using hooks likeuseEffect
.
- Communicate using HTTP requests (GET, POST, PUT, DELETE) via libraries like
fetch
or Axios. - Server-side code in Node.js can handle these requests using frameworks like Express.js, and responses can be sent back to React in JSON format.
- Virtual DOM is a lightweight copy of the actual DOM in memory.
- When the state of a React component changes, React compares the current Virtual DOM with a previous version, identifies the differences (reconciliation), and updates only the necessary parts of the actual DOM, minimizing performance overhead.
- JSX (JavaScript XML) is a syntax extension for JavaScript used in React to describe what the UI should look like.
- Allows writing HTML-like code within JavaScript, making it easier to write and understand React components.
-
Functional Components:
- JavaScript functions that accept props and return JSX.
-
Class Components:
- ES6 classes that extend
React.Component
and have additional features like state and lifecycle methods.
- ES6 classes that extend
-
Props:
- Passed from parent to child components
- Immutable within the child component
-
State:
- Managed within the component
- Can be changed using
setState
- Props are used to pass data from parent to child, while state is used for managing internal component data.
- A controlled component in React is a component whose value is controlled by React state.
- Changes to the component's value are handled by React state and are reflected in the UI.
- Typically used for form elements like input fields.
- A higher-order component in React is a function that takes a component as input and returns a new component with additional functionalities.
- Example:
withAuthentication
HOC could wrap a component and ensure that it's only rendered if the user is authenticated.
- Redux is a predictable state container for JavaScript applications.
- Maintains the application state in a single immutable state tree
- State mutations are performed by dispatching actions
- Reducers specify how the state changes in response to actions
- Facilitates predictable data flow and centralized state management.
-
Semantic Elements:
<header>
,<footer>
,<nav>
,<article>
,<section>
,<main>
-
Multimedia Elements:
<audio>
,<video>
-
Form Enhancements:
<input type="date">
,<input type="email">
,<input type="color">
- Canvas for drawing graphics
- Local Storage and Session Storage
- Version control systems: Git
- Project management tools: Jira, Trello
- IDEs: Visual Studio Code, WebStorm
- Build tools: Webpack, Parcel
- Testing frameworks: Jest, Cypress
- Deployment tools: Docker, Kubernetes
-
connect
is a function provided by the React Redux library that connects React components to the Redux store. - Accepts
mapStateToProps
andmapDispatchToProps
functions as arguments to specify how the component should interact with the store's state and actions.
-
slice
:- Returns a shallow copy of a portion of an array into a new array without modifying the original array.
-
splice
:- Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
-
map
:- Iterates over an array and returns a new array with the results of calling a provided function on every element in the calling array.
-
forEach
:- Iterates over an array and executes a provided function once for each array element, but it does not return anything.
- Redux-saga is a middleware library for Redux that aims to make application side effects (like asynchronous data fetching and impure functions) easier to manage, more efficient to execute, easy to test, and better at handling failures.
- Uses ES6 generators to write asynchronous code that looks synchronous.
-
var
:- Function-scoped
- Can be redeclared and reassigned.
-
let
:- Block-scoped
- Can be reassigned but not redeclared in the same scope.
-
const
:- Block-scoped
- Cannot be reassigned or redeclared after initialization.
-
setState
is a method provided by React'sComponent
class used to update the state of a component. - Accepts an object that represents the new state or a function that returns an object, and schedules a re-render of the component with the updated state.
- A Higher-Order Component (HOC) in React is a function that takes a component and returns a new component with additional functionalities.
- Commonly used for code reuse, cross-cutting concerns like authentication or logging, and adding state or behavior to components.
-
PureComponent
is a class component in React that extendsComponent
and implements ashouldComponentUpdate
method with a shallow prop and state comparison. - Ensures that the component only re-renders when its props or state have changed.
- Rendering in React refers to the process of converting React elements into a DOM tree and displaying them in the browser.
- When the state or props of a component change, React re-renders the component and updates the DOM efficiently using its virtual DOM algorithm.
-
React can be connected to Redux using the
connect
function provided by the React Redux library. -
Wrap your root component with the
Provider
component from React Redux to make the Redux store available to all components. -
Use the
connect
function to connect individual components to the store.
-
Stateful Components (Class Components):
- Have their own internal state managed by React
- Can change their state using
setState
.
-
Stateless Components (Functional Components):
- Do not have state
- Typically used for presentational purposes, receiving data via props and rendering UI.
- State in React can be updated using the
setState
method provided by React'sComponent
class. - You can pass an object containing the updated state properties or a function that returns such an object.
- React will then schedule a re-render of the component with the updated state.
-
Redux:
- Inspired by Flux architecture
- Simplifies it by providing a single store, unidirectional data flow, and immutable state
- Introduces concepts like reducers, actions, and middleware.
-
Flux:
- Allows multiple stores
- Supports bidirectional data flow.
- You can conditionally render a component in the render function by using JavaScript's conditional (ternary) operator or the logical
&&
operator. - Example:
render() { const isLoggedIn = this.props.isLoggedIn; return ( <div> {isLoggedIn ? <LoggedInComponent /> : <LoggedOutComponent />} </div> ); }