FDT MERN-01 (React JS) Sprint Summary

Modified on Fri, 7 Jun at 1:42 PM

Usage Directions:


This is comprehensive summary of the curriculum covered  in the MERN-01 Sprint. Revising the entire content is estimated to require one-two hours of effort, assuming you have prior exposure to the same tech stack. We recommend approaching this task in parts for a more effective revision process.


All the best!



TABLE OF CONTENTS



Topic 1: React


Why React


What is it?


React is an open-source JavaScript library used for building user interfaces and web applications..



Where is it used?


React is widely used in web development to create interactive and dynamic user interfaces. It is commonly used to build single-page applications, mobile applications, and native applications.



How is it used?


1. Install React: Start by installing React using a package manager like npm or yarn.
2. Set up the project: Create a new React project using create-react-app or set up a custom project structure.
3. Create components: Break down the UI into reusable components to improve maintainability and reusability.
4. Use JSX: Write HTML-like syntax called JSX to define the structure and appearance of the components.
5. Manage state and props: Assign state and props to components to handle data and pass it between components as needed.


Takeaways / Best Practices:


- Keep components small and modular to improve code maintainability.
- Understand and follow React's lifecycle methods to handle component updates and side effects effectively.

Code snippet:


Here is an example of a basic React component:


import React, { Component } from 'react';

class MyComponent extends Component {
render() {
return (
<div>
<h1>Hello, React!</h1>
<p>This is a basic React component.</p>
</div>
);
}
}

export default MyComponent;

In this snippet, we define a class-based component named `MyComponent` which renders an HTML structure using JSX. This component can then be used in other parts of the application.


Components in React


What is it:


Components in React are reusable building blocks that encapsulate the logic and visual structure of a user interface.



Where is it used:


- Components are used in React applications to create a modular and maintainable code structure.
- They can be used to represent any part of the user interface, such as a whole page, a section, or even a small element within a page.



- Components can be divided into two types: functional components and class components.
- Functional components are simple JavaScript functions that receive props (input data) and return JSX (rendered output).
- Class components are ES6 classes that extend the React.Component class and have a render method.


Takeaways / Best practices:

- Use functional components whenever possible, as they are simpler and can benefit from React's hooks system.
- Divide your components into presentational and container components to separate concerns.

render() Method in React


What is it? 

The render() method in React is used to render the JSX code or elements on the screen as HTML, which is the output displayed to the user.

Where is it used?


The render() method is used in React components, particularly in the class components.



How is it used?

1. The render() method is automatically called whenever the state or props of a component change.
2. Inside the render() method, JSX code is written to describe the desired component structure, UI elements, and their properties.
3. The render() method must always return a single JSX element or null.
4. React will compare the virtual DOM created from the previous render with the new virtual DOM created from the current render. It will only update the real DOM if there are any differences between them.


Takeaways / Best practices:
- The render() method should be pure, i.e., it should not modify the state or interact with the browser directly. It should only be used for rendering the UI based on the current state and props.
- Avoid complex logic or calculations within the render() method. It can impact the performance of the application.
- Use conditional rendering techniques to conditionally render different components or elements based on the state or props.



JSX



What is it:


JSX is a syntax extension for JavaScript that allows embedding HTML-like syntax within JavaScript code.

Where is it used:


JSX is primarily used in web development with React, a popular JavaScript library for building user interfaces. It is used to write dynamic and reusable UI components.

How is it used:

1. Import React: In order to use JSX, the React library needs to be imported at the beginning of the file.
2. Write JSX code: Write HTML-like code within JavaScript functions to define the structure and appearance of UI components.
3. Use curly braces for JavaScript expressions: To embed JavaScript expressions within JSX, enclose them within curly braces {}.
4. Render JSX: JSX code needs to be compiled into regular JavaScript code using a build tool like Babel before it can be rendered in the browser.

Code snippet:

import React from 'react';

function App() {
const name = 'John';
return (
<div>
<h1>Hello, {name}!</h1>
<p>Welcome to my website.</p>
</div>
);
}

export default App;


Takeaways / Best practices:


- Use proper indentation to improve code readability.
- Separate JSX code into reusable components for easier management.
- Always include a unique key prop when rendering dynamic lists in JSX.




State in React


What is it:


State in React is a built-in feature that allows components to store and manage data. It is used to keep track of a component's internal state and update it when necessary.

Where is it used:


- It is useful for handling user input, storing temporary data, and re-rendering the component when the state changes.

How is it used:


1. Initialize state in the component's constructor or using the `useState` hook.
2. Access the state value in the component's render method or functional component.
3. Update the state using the `setState` method or the state setter function from the `useState` hook.


// Example code snippet using class components:

class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}

incrementCount() {
this.setState(prevState => ({
count: prevState.count + 1
}));
}

render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.incrementCount}>Increment</button>
</div>
);
}
}


// Example code snippet using functional components and hooks:

import React, { useState } from "react";

function MyComponent() {
const [count, setCount] = useState(0);

const incrementCount = () => {
setCount(prevCount => prevCount + 1);
};

return (
<div>
<p>Count: {count}</p>
<button onClick={incrementCount}>Increment</button>
</div>
);
}


Takeaways / Best practices:
- Only use state for data that is specific to a component.
- Avoid mutating state directly; always use the proper methods for updating state.
- If multiple components need access to the same state, lift it up to a common ancestor component.
- For complex state management, consider using Redux or other state management libraries.




Handling Events in React


What is it:


Handling events in React is the process of defining and responding to user actions, such as button clicks or input changes, within a React component.


Where is it:


It is used in React components to enable interactivity and provide dynamic behavior to the user interface.

How is it used:

1. Attach the event handler to the desired DOM element using the appropriate React event syntax (e.g., onClick for a button click event).
2. In the event handler function, define the logic to be executed when the event occurs.
3. Optionally, access event information by passing it as a parameter to the event handler function.

Example code snippet:

import React from 'react';

class Button extends React.Component {
handleClick() {
console.log('Button clicked!');
}

render() {
return (
<button onClick={this.handleClick}>Click me</button>
);
}
}

export default Button;


Takeaways / best practices:

- Always make sure to bind event handler functions to the correct instance of the component using either class properties syntax or by using the `bind()` method. This ensures that the `this` keyword refers to the component instance within the event handler.
- Use arrow functions to avoid binding issues with the `this` keyword.
- When accessing event information, use the synthetic event object provided by React, which normalizes browser differences. Access specific event properties as needed, such as `event.target.value` for input value changes.



Props in React


What is it? 


Props in React are a way to pass data from a parent component to its child components.

Where is it used? 


Props are used in React components to share data between different parts of the application.

How is it used? 


1. Props are passed from parent component to child component using attributes.
2. The child component receives the props as a parameter in its function or class definition.
3. The child component can access the passed props and use it to render UI or perform actions.


// Parent Component:

function App() {
return (
<div>
<ChildComponent name="John" age={25} />
</div>
);
}

Child Component:

function ChildComponent(props) {
return (
<div>
<h1>Hello {props.name}</h1>
<p>Your age is {props.age}</p>
</div>
);
}


Takeaways / Best Practices:
- Props should be used to pass data downwards from parent to child components.
- Props should be treated as read-only, meaning child components should not modify their props.
- It is a good practice to define default values for props using defaultProps in case the parent component does not pass any value.




ES6 Modules


What is it:


ES6 Modules is a feature of ECMAScript 6 (ES6) that allows developers to create reusable and organized code by exporting and importing functionality between different JavaScript files.

How it is used:

1. Exporting:
- In the file that contains the functionality to be exported, use the `export` keyword followed by the specific function, variable, or class to be exported.
- You can also use the `default` keyword to export a single primary function or object as the default export.

2. Importing:
- In the file where you want to use the exported functionality, use the `import` keyword followed by the name of the exported item or items enclosed in curly braces `{}`.
- You can import multiple modules in a single import statement.
- If using a default export, you can assign it to a variable or directly use it without assigning to any variable.

Example code snippet:


File: math.js

export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
export const multiply = (a, b) => a * b;
export const divide = (a, b) => a / b;


File: main.js

import { add, subtract } from './math.js';

console.log(add(5, 3)); // Output: 8
console.log(subtract(10, 4)); // Output: 6


Takeaways / Best practices:
- Use separate modules for different functionalities to keep the code organized and manageable.
- Explicitly import only what is needed instead of importing the entire module.
- When multiple items need to be imported, it is preferable to list them individually instead of using the `*` wildcard.



Named/Default Exports


What is it? 


Named/Default exports allow us to export and import multiple values from or into a module in JavaScript. With named exports, we can export multiple values from a module and import them using their respective names. With default exports, we can explicitly select a single value to be exported from a module and import it without the need for curly braces.

Where is it used?


Named/Default exports are commonly used in JavaScript modular development, where we split our code into separate modules to promote reusability and maintainability.

How is it used?
1. To create named exports, use the `export` keyword before each value that you want to export from a module.
2. To import named exports, use the `import` keyword followed by the names of the values you want to import from the module, enclosed in curly braces `{ }`.
3. To create a default export, use the `export default` statement before the value that you want to be the default export of the module.
4. To import a default export, use the `import` keyword followed by any name of your choice, followed by the `from` keyword and the path to the module. No curly braces are needed for default imports.

Code snippet to explain the same:

// module.js
export const name = 'John';
export const age = 25;
export const country = 'USA';
export default class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}

// main.js
import { name, age, country } from './module.js';
import Person from './module.js';

console.log(name); // 'John'
console.log(age); // 25
console.log(country); // 'USA'

const person = new Person('Alice', 30);
console.log(person.name); // 'Alice'
console.log(person.age); // 30



Takeaways / best practices:
- Use named exports when you want to export multiple values from a module and import them using their specific names.
- Use default exports when you want to export a single value from a module and import it without using curly braces.
- Avoid mixing named and default exports in the same module for clarity and readability.




Handling Forms in React


What is it:

Handling forms in React refers to the process of managing user input and interaction with HTML form elements within a React component.

Where is it used:


It is used in React applications that require the collection and submission of user data, such as sign-up forms, contact forms, and search forms.



How is it used:

  1. Create a form component: Build a separate component that represents the form and contains its own state to track the input values.

  2. Set up event handlers: Attach event handlers to the form elements to capture user input and update the component's state accordingly. Common event handlers for form elements include onChange, onSubmit, and onBlur.

  3. Update the component's state: Whenever an event is triggered, update the corresponding input's value in the component's state using setState(). This allows for real-time rendering of the form inputs.

  4. Handle form submission:
 Implement a submit handler function to handle the form submission event. This function can perform actions like data validation, making API calls, or updating the application state based on the form data.

  5. Prevent default behavior: Use 
event.preventDefault() within the submit handler function to prevent the default form submission behavior, which would cause a page refresh.



- Here is an example code snippet that demonstrates handling forms in React:



import React, { useState } from 'react';

const FormComponent = () => {
const [formData, setFormData] = useState({
name: '',
email: '',
});

const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};

const handleSubmit = (e) => {
e.preventDefault();
console.log(formData);
};

return (
<form onSubmit={handleSubmit}>
<label>
Name:
<input type="text" name="name" value={formData.name} onChange={handleChange} />
</label>
<label>
Email:
<input type="email" name="email" value={formData.email} onChange={handleChange} />
</label>
<button type="submit">Submit</button>
</form>
);
};

export default FormComponent;


Takeaways / Best practices:
  Use controlled components: Ensure that the form inputs are controlled components, meaning their values are managed by React's state. This allows for greater control and validation of user input.
  
Implement validation: Validate the form data before submission to ensure the data meets the required criteria.
 


Controlled Components


Controlled components are a type of form input elements that are controlled by React.js rather than the DOM.

They are used in React.js applications for building interactive forms where the form data is handled by the component's state. This allows for synchronized state management and enables developers to easily manipulate and validate form data.

Steps to use controlled components:
1. Create a state variable to store the value of the input element.
2. Set the value of the input element to this state variable.
3. Create an event handler function to handle changes in the input element's value.
4. Update the state variable with the new value whenever the input element's value changes.

Example code snippet:

import React, { useState } from "react";

const ControlledComponent = () => {
const [inputValue, setInputValue] = useState("");

const handleChange = (event) => {
setInputValue(event.target.value);
};

return (
<div>
<input
type="text"
value={inputValue}
onChange={handleChange}
/>
</div>
);
};

export default ControlledComponent;



Takeaways / Best Practices:
- Use controlled components when you need to have full control over form inputs and their state.
- Use state variables and event handlers to manage and update the value of controlled components.
- Always set the `value` prop of the input element to the corresponding state variable.
- Use the `onChange` event to update the state variable with the new value.



Lifecycle Methods in React


What is it:


Lifecycle methods in React are special methods that are executed at different stages in the lifecycle of a React component.

Where is it used:


They are used in React class components.

How is it used:

1. Mounting Phase:
   - `constructor()`: Used for initializing state and binding event handlers.
   - `render()`: Responsible for rendering the component.
 
  - `componentDidMount()`: Invoked immediately after the component is mounted (added to the DOM). Used for side effects like fetching data from an API.

2. Updating Phase:
   - `shouldComponentUpdate(nextProps, nextState)`: Determines whether the component should be re-rendered or not based on the changes in props or state.
   - `render()`: Rerenders the component.
   - `componentDidUpdate(prevProps, prevState)`: Invoked immediately after the component has been updated. Used for performing side effects after a update is done.

3. Unmounting Phase:
   - `componentWillUnmount()`: Invoked immediately before a component is unmounted and destroyed. Used for cleaning up resources, event listeners, etc.

Here is an example code snippet showcasing the usage of lifecycle methods:


class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}

componentDidMount() {
document.title = `Count: ${this.state.count}`;
}

componentDidUpdate() {
document.title = `Count: ${this.state.count}`;
}

componentWillUnmount() {
// Cleanup code here
}

handleClick() {
this.setState(prevState => ({ count: prevState.count + 1 }));
}

render() {
return (
<div>
<button onClick={() => this.handleClick()}>Increment</button>
<p>Count: {this.state.count}</p>
</div>
);
}
}


Some key takeaways and best practices for using lifecycle methods in React are:
- Avoid using deprecated lifecycle methods as they may be removed in future versions of React.
- Be careful when using `setState` inside lifecycle methods to prevent infinite loops.

Class Based components in React


What is it:


Class-based components in React are JavaScript classes that extend the base React.Component class, allowing you to create components with additional functionality and state management.

Where is it used:


- Class-based components are used in React to create more complex components that require state management and lifecycle methods.
- They are used when you need to manage state, access lifecycle methods, or implement custom methods within your component.


How is it used:

1. Create a JavaScript class that extends the React.Component class.
2. Define a render method that returns the JSX structure of the component.
3. Implement any additional functionality or custom methods within the class.
4. Use the component by including it in other components using its defined tag name.

Code snippet example:

import React, { Component } from 'react';

class MyComponent extends Component {
constructor() {
super();
this.state = {
message: 'Hello World'
};
}

componentDidMount() {
console.log('Component mounted');
}

handleClick() {
this.setState({ message: 'Button clicked' });
}

render() {
return (
<div>
<h1>{this.state.message}</h1>
<button onClick={() => this.handleClick()}>Click me</button>
</div>
);
}
}

export default MyComponent;


Takeaways / Best practices:
- Class-based components are suitable for components with complex state management needs or requiring lifecycle methods.
- It is recommended to use functional components with hooks unless there is a specific need for class-based components.
- Use lifecycle methods (e.g., componentDidMount) for performing side effects like fetching data or subscribing to external events.
- Always call `super()` in the constructor of a class-based component to ensure the parent class (React.Component) is properly initialized.



constructor() in Class Based Components


What is it:


Constructor() is a special method in Class Based Components in React that is automatically called when an instance of the component is created or initialized.

Where is it used:


It is used to initialize the state and bind event handlers in class components.

How is it used:
1. Declare the constructor method within the class component.
2. Inside the constructor, use the super() method to call the constructor of the parent class.
3. Initialize the state of the component by assigning an object to this.state.
4. Bind event handlers to the current instance of the component using the .bind() method, to avoid losing the context of "this" when the event handlers are called.

- Example of using constructor():

  

class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
this.handleClick = this.handleClick.bind(this);
}

handleClick() {
this.setState({
count: this.state.count + 1
});
}

render() {
return (
<div>
<button onClick={this.handleClick}>Increment</button>
<p>Count: {this.state.count}</p>
</div>
);
}
}



Takeaways / best practices:
- Use the constructor to initialize the component's state and bind event handlers.
  - Avoid using the constructor for asynchronous actions or side effects, as it may cause performance issues.



componentDidMount()


What is it? 
componentDidMount() is a lifecycle method in React that is called immediately after a component is mounted or rendered on the DOM.

Where is it used?
It is used in React components where we need to perform some actions or initialize variables after the component has been added to the DOM.

How is it used?
1. Declare and define the componentDidMount() method inside your React component.
2. Inside componentDidMount(), perform the necessary operations or actions that need to be executed after the component has been mounted.
3. You can use setState() to update the component's state if needed.
4. It is also commonly used for making API calls or subscribing to events.

Code snippet:

class MyComponent extends React.Component {
componentDidMount() {
// Perform actions after the component has been mounted
console.log('Component has been mounted');

// Fetch data from an API
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
this.setState({ data });
});
}

render() {
return (
<div>
{/* Component's rendering code */}
</div>
);
}
}


- Takeaways / best practices:
- Use componentDidMount() when you need to perform tasks after the component is mounted on the DOM.
  - Avoid long-running operations in componentDidMount as it may cause delay in the rendering process.


componentDidUpdate()


What is it?


componentDidUpdate() is a lifecycle method in React that is invoked immediately after an update occurs, specifically after the component's update has been rendered on the screen.


Where is it used?


componentDidUpdate() is used in React class components where the component needs to perform some actions after an update, such as fetching new data, updating the DOM, or interacting with external libraries.

-How is it used? (bulleted steps):


1. Implement the componentDidUpdate() method in your React class component.
2. Inside the componentDidUpdate() method, write the logic to perform the desired actions after an update.
3. Make sure to include necessary conditions to prevent an infinite loop when updating the component.

Takeaway / Best practices:

- Always check the previous and current props and state values using conditionals or a deep comparison to determine if specific actions need to be taken.
- Avoid causing an infinite update loop by only updating the component under certain conditions.


componentWillUnmount()


- What is it?


- `componentWillUnmount()` is a lifecycle method in React that is called just before a component is unmounted and removed from the DOM.

- Where is it used?


- It is used in React class components to perform any necessary cleanup tasks before a component is removed from the UI.

- How is it used?


1. Use the `componentWillUnmount()` method in a class component.
2. Inside the method, you can perform any cleanup actions such as canceling timers, removing event listeners, or unsubscribing from external services.
3. Once the component is unmounted, the `componentWillUnmount()` method is called automatically.


- Takeaways / best practices:
- `componentWillUnmount()` is useful for cleaning up any resources used by the component.
   - It should be used to cancel any pending network requests, remove event listeners, or unsubscribe from external services.




Functional Components in React


What is it? 
Functional Components in React are lightweight and simple components that are defined as JavaScript functions. They are used to render small, isolated UI elements.

Where is it used? 


Functional Components are used throughout React applications to create reusable UI elements. They are commonly used for displaying static content or handling user interactions.

How is it used? 


1. Define a functional component as a JavaScript function, taking in the necessary props as arguments.
2. Within the function body, write the UI code using HTML-like syntax called JSX.
3. Add logic and event handlers within the function as needed.
4. Return the JSX code to render the component.

Example code snippet for a simple functional component:

import React from 'react';

const MyComponent = ({ name }) => {
return (
<div>
<h1>Hello, {name}!</h1>
</div>
);
};

export default MyComponent;

- Takeaways / Best practices
1. Functional Components are preferred over class components when possible because they are simpler, easier to read, and better for performance.
2. Use React hooks like useState and useEffect within functional components to manage state and side effects.


Hooks in React


What is it? 


Hooks in React are a new feature that allows developers to use state and other React features in functional components instead of class components.

Where is it used?


Hooks can be used in any React application to manage state, perform side effects, and enable code reuse in functional components.

How is it used? 
1. Import the hook you want to use from the "react" library.
2. Call the hook function inside a functional component. This can be done multiple times for different hooks.
3. Use the state or other features provided by the hook in the component's logic.
4. Update the state or perform any other actions defined by the hook whenever necessary.

Code snippet:


import React, { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);

const increment = () => {
setCount(count + 1);
}

return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}


Takeaways / best practices:
- Hooks should always be called at the top level of a functional component and not inside loops, conditions, or nested functions.
- It is recommended to use hooks for state management and side effects instead of relying on class components and lifecycle methods.

useState Hook


What is it:


useState is a hook in React that allows functional components to have state variables.

Where is it used:


- It is used in React functional components to add stateful values to the component.
- useState is typically used when we need to store and manipulate state within a functional component.


How is it used:


- useState returns a pair of values: the current state value and a function to update the state.
- To use useState, we import it from the 'react' library, and then call it within our component, passing in an initial value for the state..
- The state value can be accessed and updated using the variable and the update function respectively.
- It is important to note that when the state is updated using the update function, the component will re-render to reflect the updated state.



Example code snippet:

import React, { useState } from 'react';

const ExampleComponent = () => {
const [count, setCount] = useState(0); // using useState to add a state variable 'count' with initial value 0

const incrementCount = () => {
setCount(count + 1); // updating the state using the setCount function
};

return (
<div>
<p>Count: {count}</p>
<button onClick={incrementCount}>Increment</button>
</div>
);
};

export default ExampleComponent;


Takeaways / best practices:


- useState is a powerful hook that simplifies state management in functional components.
- It is recommended to use a meaningful variable name when destructuring the returned values from useState for easier understanding.
- When updating the state, it is generally better to use the functional form of setState (e.g., setCount(prevCount => prevCount + 1)) to avoid any issues related to current closures and stale state values.



Conditional Rendering


What is it:


- Conditional rendering is the process of rendering different outputs based on certain conditions or criteria.

Where is it used:


- Conditional rendering is commonly used in web development, especially when building dynamic and interactive user interfaces.

- Steps to use conditional rendering:

1. Define the condition or criteria based on which the rendering will be determined.
2. Use a conditional statement (such as if, switch, or ternary operator) to evaluate the condition.
3. Specify the rendering output for each possible condition or criteria.

- Example code snippet in React:

function App() {
const isLoggedIn = true;

return (
<div>
{isLoggedIn ? (
<h1>Welcome, User!</h1>
) : (
<h1>Please log in.</h1>
)}
</div>
);
}


Best practices and takeaways:

1. Keep the logic for conditional rendering as simple and concise as possible.
2. Use meaningful variable names to improve code readability.
3. Break down complex conditions into smaller, more manageable parts.



Lists in React


What is it:


Lists in React are a way to render a collection of elements dynamically.

Where is it used:


Lists are commonly used in React whenever there is a need to render multiple items from an array or any other collection.

How is it used:


- Create an array or collection of items that you want to render.
- Use the map() method on the array to iterate over each item and return JSX for rendering.
- Assign a unique key to each rendered element. This helps React efficiently update the list when changes occur.
- Render the list of elements using curly braces {} in the JSX.

Example code:



const fruits = ["apple", "banana", "orange"];

const FruitList = () => {
return (
<ul>
{fruits.map((fruit, index) => (
<li key={index}>{fruit}</li>
))}
</ul>
);
};

export default FruitList;

Takeaways / best practices:
- Always provide a unique key for each rendered element within a list to optimize React's performance.
- When using an index as the key, ensure that the list remains static and doesn't change in order or length. Otherwise, a stable identifier should be used.
- It's recommended to extract the list rendering logic into a separate component for reusability and better code organization.



Arrays.map()


What is it:


- Arrays.map() is a built-in JavaScript method that creates a new array by performing a specified operation on each element of an existing array.


Where is it used:


  • Rendering Lists: One of the most common uses of Array.map() is to create a list of elements in the UI. 

  • Data Transformation: Array.map() can be used to transform data before rendering it. For example, you might use Array.map() to add a calculated field to each item in an array, or to convert strings to JSX elements.

  • Creating Select Options: Array.map() can be used to create the options of a <select> input. You might have an array of values, and use Array.map() to transform them into an array of <option> elements.

  • Table Rows Generation: In case of tabular data, Array.map() can be used to generate the rows of a table. Each object in an array might correspond to a row in the table, with the properties of the object corresponding to the cells in the row.


How is it used:


1. Define an array with some elements.
2. Use the map() method on the array and pass a callback function as an argument.
3. The callback function takes each element of the array as its parameter and performs the desired operation.
4. The map() method returns a new array with the modified values based on the specified operation.

Code snippet example:

const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = numbers.map((num) => {
return num * 2;
});

console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]


Takeaways / best practices:
- Use map() when you need to construct a new array with modified elements based on an existing array.
- The callback function passed to map() should return the modified value for each element.
- Remember that map() does not modify the original array, it returns a new array.



Arrays.forEach()


What is it?


Arrays.forEach() is a method in JavaScript that allows you to loop over the elements of an array and execute a provided function for each element.

Where is it used?


It is commonly used in JavaScript for iterating over all the elements of an array and performing some operation or manipulation on each element.

How is it used?


1. Declare an array that you want to iterate over.
2. Define a callback function that will be executed for each element in the array.
3. Call the forEach() method on the array and pass the callback function as an argument.
4. The callback function will be called for each element, with the current element, index, and the original array as arguments.
5. Inside the callback function, perform the desired operation on the current element.

Code snippet:

const numbers = [1, 2, 3, 4, 5];

numbers.forEach(function(number, index, array) {
console.log("Number:", number);
console.log("Index:", index);
console.log("Array:", array);
});


Takeaways / best practices:
- Use forEach() when you want to iterate over all the elements of an array and perform a specific task on each element.
- The callback function passed to forEach() can have three parameters: the current element, its index, and the original array. Use them according to your needs.


Keys in React


What is it?

Keys in React are unique identifiers used to optimize the rendering process when dealing with lists or collections of elements.

Where is it used?

Keys are used whenever React is rendering a list or collection of similar elements, like an array of data that needs to be displayed as a list in the UI.

How is it used?

  1. When creating a list of components, each component should be given a prop called 'key' that has a unique value. This is usually done in the method that maps the data to components.

  2. This key is typically derived from the data itself. For example, if your data is a list of objects with unique ids, then those ids are great candidates for keys.

  3. If no unique identifier is available, React can use the index of each item in the array as a key, although this is not recommended due to performance and consistency issues.

Here is a code snippet that demonstrates this:

const tasks = [
{ id: 1, task: 'Take out the trash' },
{ id: 2, task: 'Wash the dishes' },
{ id: 3, task: 'Do the laundry' },
];

// In your render method...
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>{task.task}</li>
))}
</ul>
);

Takeaways / Best Practices

  • Always provide a key when creating lists of elements in React. This allows React to optimize re-rendering by quickly identifying which items have changed, are added, or are removed.

  • Keys should be unique among siblings. They don't need to be globally unique in the entire application.

  • Avoid using indices as keys if the order of items may change. This can negatively impact performance and may cause issues with the component state.


React Router


What is it?

React Router is a library for managing and handling routing in React applications, allowing you to create multiple pages without reloading the browser.

Where is it used?

React Router is used in single-page applications (SPAs) where different views or "pages" are required. These applications use client-side routing instead of the traditional server-side routing.

How is it used?

  1. First, you have to install the React Router library into your project using either npm or yarn.

  2. Import the necessary components from 'react-router-dom', such as BrowserRouterRoute, and Routes.

  3. Wrap your entire application or the relevant part of it in a BrowserRouter component.

  4. Use the Route component to define different routes. The path prop corresponds to the URL, and the element prop corresponds to the component that should be rendered at that path.

  5. Use the Routes component to wrap your Route components.


Here is a basic code snippet demonstrating its usage:

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Home from "./Home";
import About from "./About";

function App() {
return (
<Router>
<Routes>
<Route path="/about" element={<About />} />
<Route path="/" element={<Home />} />
</Routes>
</Router>
);
}

export default App;


Takeaways / Best Practices

  • Always wrap your entire application or the part that requires routing in a BrowserRouter component.

  • For paths, be mindful of the exact attribute. By default, routes in React Router v6 are exact and relative.

  • For dynamic routing, you can use URL parameters with the useParams hook.

  • To navigate programmatically, use the useNavigate hook.

  • Keep your application’s URL and state synchronized by using the Link or NavLink component for navigation. Avoid using <a> tags as they cause a full page refresh.

  • When migrating from a version of React Router earlier than v6, note the change from component to element in Route and the replacement of Switch with Routes.

useEffect Hook



What is it: 


- useEffect is a Hook in React that allows you to perform side effects in functional components.


Where is it used:


- It is used in functional components to handle side effects, such as data fetching, subscriptions, or manually changing the DOM.

How is it used:
1. Import the useEffect Hook from React.
2. Define a functional component.
3. Inside the functional component, call the useEffect Hook.
4. Pass a callback function as the first argument to useEffect. This callback function will be executed after the component has rendered.
  5. Optionally, you can pass a second argument to useEffect. This second argument is an array of dependencies, which specify when the effect should be re-run. If the dependencies change, the effect will re-run.



- Code snippet:


import React, { useEffect } from 'react';

function MyComponent() {
useEffect(() => {
console.log('Component has rendered');
// Additional logic or side effects can be performed here
return () => {
console.log('Component will unmount');
// This cleanup function will be executed when the component is unmounted
};
}, []); // Empty dependency array means the effect will only run once, when the component mounts

// JSX for the component...
}


Best practices:
- Specify all dependencies in the dependency array to ensure proper re-rendering and cleanup.
- Use cleanup functions within useEffect to handle any necessary cleanup tasks.
- Avoid making API calls or performing heavy computations directly in the useEffect callback. Instead, consider moving them to separate functions or custom hooks.


Fetching API Data


What is it:


Fetching API data is the process of retrieving data from an external API (Application Programming Interface) through an HTTP request.

Where is it used:


It is used in various applications and websites that require real-time or dynamic data from external sources such as weather information, stock prices, social media posts, or database records.

How is it used:


1. Constructing the API request URL with the necessary parameters and endpoint.
2. Initiating an HTTP request to the API endpoint using methods like GET, POST, PUT, or DELETE.
3. Handling the response received from the API.
4. Parsing the response data, which can be in various formats like JSON, XML, or CSV.
5. Utilizing the retrieved data in the application or website as required.

Example code snippet using JavaScript fetch() method:

fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
// Process the retrieved data
console.log(data);
})
.catch(error => {
// Handle any errors
console.error('Error:', error);
});


Takeaways / best practices:


- Always check the API documentation for any required headers, authentication, or query parameters.
- Handle errors properly to prevent app crashes or unexpected behavior.
- Consider using async/await syntax or library wrappers for a more readable and organized code structure.
- Implement proper error handling and fallbacks to handle situations like slow network or server errors.
- Be mindful of rate limits or usage restrictions imposed by the API provider.


Lifting State up in React


What is it:


Lifting state up in React is the process of moving the state data from a child component to its parent component, allowing the parent to have control over the child components' state.

Where is it used:


Lifting state up is used in React when multiple child components need to share the same state data or when the state needs to be accessed or modified by multiple components.


How is it used:


1. Identify the shared state that needs to be lifted up.
2. Lift the state up by moving it to the parent component.
3. Pass the state and any required callback functions to the child components as props.
4. Modify any child components to use the passed props instead of their local state.
5. Handle state updates and modifications in the parent component and pass them down to the child components as props.

Here's a code snippet to illustrate the concept of lifting state up:

// ChildComponent.js
import React from "react";

const ChildComponent = ({ count, increment }) => {
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};

export default ChildComponent;

// ParentComponent.js
import React, { useState } from "react";
import ChildComponent from "./ChildComponent";

const ParentComponent = () => {
const [count, setCount] = useState(0);

const increment = () => {
setCount(count + 1);
};

return (
<div>
<h1>Parent Component</h1>
<ChildComponent count={count} increment={increment} />
</div>
);
};


export default ParentComponent;

In this example, the count state is being lifted up from the ChildComponent to the ParentComponent. The ParentComponent controls the state and passes it down to the ChildComponent as a prop, along with the increment function. The ChildComponent then uses these props to display the count and trigger the increment when the button is clicked.

Takeaways / Best practices:
- Lifting state up helps in centralizing and managing the state of multiple components from a common parent component.
- It improves code reusability and reduces redundancy.
- Be mindful of the component hierarchy and decide the appropriate level at which the state should be lifted up.




Additional Topics


Real DOM vs Virtual DOM



Real DOM

What is it?

  • Real DOM (Document Object Model) is a programming interface for web documents. It represents the structure of a document as a tree of objects.

Where is it used?

  • It's used in web browsers to represent the page so that programs can change the document structure, style, and content.

How is it used?

  • Manipulating the Real DOM directly through native JavaScript methods.

  • Changes are directly written to the Real DOM.

  • Any change leads to re-rendering of the entire tree structure.

Takeaways / Best Practices:

  • Direct manipulation of the Real DOM can be slow and inefficient.

  • It's often better to use a library like React that optimizes DOM manipulation.

Virtual DOM


What is it?

  • Virtual DOM is a lightweight copy of the Real DOM, existing only in memory.

Where is it used?

  • In React, the Virtual DOM is used to optimize rendering and improve performance.

How is it used?

  1. Create a Virtual DOM Tree: React creates a Virtual DOM tree that mirrors the Real DOM.

  2. Detect Changes: When a change occurs, React creates a new Virtual DOM tree.

  3. Compare with Previous Tree: React compares the new Virtual DOM tree with the previous one using a process called "Reconciliation."

  4. Update Real DOM: Only the differences between the two Virtual DOM trees are updated in the Real DOM.

Code snippet to explain the same:

function MyComponent(props) {
return <h1>Hello, {props.name}!</h1>;
}

// React will manage the Virtual DOM and only update the Real DOM where necessary
ReactDOM.render(<MyComponent name="World" />, document.getElementById('root'));


Takeaways / Best Practices:

  • Virtual DOM provides a more efficient way to update the view in a web application.

  • It allows for a smoother user experience by minimizing direct manipulation of the Real DOM.

  • Utilizing React's Virtual DOM system abstracts away much of the complexity of direct DOM manipulation, allowing developers to focus on building their application.


Stateful and Stateless components


Stateful Components

What is it?

  • A Stateful Component is a component that manages and maintains its own state, allowing it to have a memory of past rendered outputs.

Where is it used?

  • Used in React applications where components need to maintain information that changes over time, such as user input, UI state, or data from an API.

How is it used?

  1. Initialize State: Define the initial state within the component's constructor or using the useState hook.

  2. Update State: Use methods like setState or the state updater function from useState to modify the state.

  3. Render Based on State: The component re-renders whenever the state changes, reflecting the new state in the UI.

Code snippet to explain the same:

class MyComponent extends React.Component {
constructor() {
super();
this.state = { count: 0 };
}

increment = () => {
this.setState({ count: this.state.count + 1 });
};

render() {
return (
<div>
<button onClick={this.increment}>Increment</button>
<p>Count: {this.state.count}</p>
</div>
);
}
}


Takeaways / Best Practices:

  • Use Stateful Components when you need to manage dynamic data within a component.

  • Be mindful of unnecessary re-renders, as they can affect performance.

Stateless Components

What is it?

  • A Stateless Component is a component that does not manage or maintain any internal state, relying solely on props passed down from a parent component.

Where is it used?

  • Used in React applications for components that simply receive data and render UI without managing or modifying that data.

How is it used?

  1. Receive Props: Accept data through props from a parent component.

  2. Render Based on Props: Render the UI based on the received props without maintaining any internal state.

Code snippet to explain the same:

function MyComponent(props) {
return <h1>Hello, {props.name}!</h1>;
}


Takeaways / Best Practices:

  • Stateless Components are simpler and easier to test and maintain.

  • Use Stateless Components for presentational logic, keeping the component hierarchy clean and efficient.

  • Combining Stateless Components with Stateful Components allows for a clear separation of concerns within a React application, promoting maintainable and scalable code.


Higher Order Components


Higher-Order Components (HOCs)

What is it?

  • A Higher-Order Component (HOC) is a function that takes a component and returns a new component with additional props or behavior.

Where is it used?

  • Used in React applications to reuse component logic, enhance components with additional functionality, or modify props.

How is it used?

  1. Define the HOC: Create a function that accepts a component and returns a new component with added functionality or modified props.

  2. Wrap the Component: Use the HOC to wrap the component you want to enhance.

  3. Pass Props: Pass any necessary props to the HOC, which can then be used or modified within the wrapped component.

  4. Render the Wrapped Component: The HOC can render the wrapped component with additional props or behavior.

Code snippet to explain the same:

// HOC that adds 'isAuthenticated' prop
function withAuthentication(WrappedComponent) {
return function(props) {
// Add or modify props as needed
const newProps = { ...props, isAuthenticated: true };
return <WrappedComponent {...newProps} />;
};
}

// Usage with a regular component
function MyComponent(props) {
return <div>{props.isAuthenticated ? 'Authenticated' : 'Not Authenticated'}</div>;
}

const EnhancedComponent = withAuthentication(MyComponent);

// Render the enhanced component
ReactDOM.render(<EnhancedComponent />, document.getElementById('root'));


Takeaways / Best Practices:

  • HOCs provide a way to write reusable and clean code by abstracting shared logic.

  • Be mindful of prop collisions, where the HOC might override props that the wrapped component is expecting.

  • While HOCs are powerful, the introduction of React Hooks has provided alternative ways to share logic between components. Consider the use case and choose the approach that best fits the needs of your application.


Redux


Redux

What is it?

  • Redux is a predictable state management library that helps you write applications that behave consistently across different environments and are easy to test.

Where is it used?

  • Used in JavaScript applications, particularly with React, to manage and maintain the application's global state.

How is it used?

  1. Create a Store: The store holds the whole state tree of the application.

  2. Define Actions: Actions are payloads of information that send data from the application to the store.

  3. Create Reducers: Reducers specify how the application's state changes in response to actions.

  4. Dispatch Actions: Use the dispatch method to send actions to the store.

  5. Connect Components: Use the connect function to connect React components to the Redux store.

  6. Use Middleware (Optional): Enhance the store with middleware like Redux Thunk for handling asynchronous actions.

Code snippet to explain the same:

// Action
const increment = () => ({ type: 'INCREMENT' });

// Reducer
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
default:
return state;
}
};

// Create Store
const store = Redux.createStore(counterReducer);

// Dispatch Action
store.dispatch(increment());

// React Component
const Counter = (props) => <div>{props.count}</div>;

// Connect to Redux Store
const mapStateToProps = (state) => ({ count: state });
const ConnectedCounter = ReactRedux.connect(mapStateToProps)(Counter);


Takeaways / Best Practices:

  • Redux provides a single source of truth for the application's state, making it easier to debug and inspect.

  • Keep reducers pure and free from side effects to maintain predictability.

  • Use middleware like Redux Thunk or Redux Saga for handling complex asynchronous logic.

  • Be mindful of the complexity that Redux can introduce; it may be overkill for simple applications.

  • Utilize tools like Redux DevTools for enhanced development and debugging experience.

  • Structure the Redux store thoughtfully, keeping in mind the scalability and maintainability of the application.

  • Consider using modern Redux toolkits like @reduxjs/toolkit to simplify common Redux use cases and reduce boilerplate code.


React Components


React Components

What is it?

  • React Components are independent, reusable pieces of code that represent a part of the UI in a React application.

Where is it used?

  • Used in React applications to build and organize the user interface.

How is it used?

  1. Define a Component: Create a component using either a function or a class.

  2. Pass Props: Pass properties (props) to the component to customize its behavior and appearance.

  3. Use State (Optional): Manage internal state within the component for dynamic behavior.

  4. Render the Component: Return the JSX that defines how the component should be rendered.

  5. Compose Components: Combine multiple components to build complex UI structures.

Code snippet to explain the same:

// Function Component
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}

// Class Component
class Counter extends React.Component {
state = { count: 0 };

increment = () => {
this.setState({ count: this.state.count + 1 });
};

render() {
return (
<div>
<button onClick={this.increment}>Increment</button>
<p>Count: {this.state.count}</p>
</div>
);
}
}

// Using the Components
ReactDOM.render(
<>
<Greeting name="World" />
<Counter />
</>,
document.getElementById('root')
);


Takeaways / Best Practices:

  • Components are the building blocks of a React application, promoting reusability and maintainability.

  • Use functional components with hooks for simpler code, unless you need features exclusive to class components.

  • Keep components small and focused on a single responsibility to make them easier to understand and test.

  • Pass data down through props and manage state at the appropriate level in the component hierarchy.

  • Consider using memoization techniques like React.memo for optimizing performance in case of frequent re-renders.

  • Follow a consistent naming and structuring convention to make the codebase more navigable and maintainable.


Prop Drilling


Prop Drilling

What is it?

  • Prop drilling is the process of passing data through multiple layers of components by using props, even if intermediate components don't need the data.

Where is it used?

  • Used in React applications where a deeply nested component needs access to specific data, and there's no global state management like Redux.

How is it used?

  1. Pass Props to Child: Pass the required data as props to a child component.

  2. Forward Props to Grandchild: If the child component doesn't need the data, it forwards the props to its child (grandchild of the original component).

  3. Repeat as Needed: Continue this process through as many levels as needed until reaching the component that requires the data.

  4. Use the Props in Target Component: The deeply nested component that needs the data can then use the props as usual.

Code snippet to explain the same:

function Parent(props) {
return <Child someProp={props.someProp} />;
}

function Child(props) {
// Doesn't use 'someProp', just passes it along
return <Grandchild someProp={props.someProp} />;
}

function Grandchild(props) {
// Uses 'someProp'
return <div>{props.someProp}</div>;
}

// Usage
ReactDOM.render(<Parent someProp="Value" />, document.getElementById('root'));


Takeaways / Best Practices:

  • Prop drilling can lead to maintenance challenges, as changes to the prop structure can require updates at many levels.

  • It can make the code harder to follow, as data is passed through components that don't need it.

  • Consider using context or global state management (like Redux) for sharing data across many levels of the component tree, especially if prop drilling becomes cumbersome.

  • If prop drilling is used, ensure that the prop names and types are consistent and well-documented to minimize confusion.


Server Side Rendering vs Client Side Rendering


Server-Side Rendering (SSR)

What is it?

  • SSR is the process of rendering the entire HTML content on the server and sending it to the client's browser.

Where is it used?

  • Used in web applications to improve initial page load time, enhance SEO, and provide content to users even if JavaScript is disabled.

How is it used?

  1. Request Handling: The server receives a request for a webpage.

  2. Render HTML on Server: The server processes the request, fetches data if needed, and renders the HTML.

  3. Send HTML to Client: The fully rendered HTML is sent to the client's browser.

  4. Display Static Content: The browser displays the static content, and JavaScript can be used to add interactivity.

Takeaways / Best Practices:

  • SSR can improve SEO and initial load performance but may increase server load.

  • Be mindful of potential mismatches between server-rendered and client-rendered content.

  • Utilize caching strategies to enhance performance.

Client-Side Rendering (CSR)


What is it?

  • CSR is the process of rendering the web application entirely in the client's browser, usually through JavaScript.

Where is it used?

  • Used in web applications where interactivity and rich user experiences are prioritized, especially in Single Page Applications (SPAs).

How is it used?

  1. Send Empty HTML: The server sends an empty or minimal HTML file with links to JavaScript files.

  2. Load JavaScript: The browser downloads and executes the JavaScript, which contains the rendering logic.

  3. Render on Client: The client's browser renders the components and builds the DOM.

  4. Handle User Interactions: Subsequent user interactions are handled entirely on the client, often without reloading the page.

Code snippet to explain the same (for a generic JavaScript application):

<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<script src="app.js"></script>
</head>
<body>
<div id="app"></div>
</body>
</html>

// app.js
document.getElementById('app').innerHTML = '<h1>Hello, World!</h1>';


Takeaways / Best Practices:

  • CSR provides a more interactive user experience but may have slower initial load times.

  • Consider using techniques like code splitting and lazy loading to optimize performance.

  • Be aware that CSR may have SEO challenges, as search engine crawlers might not execute JavaScript.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article