IP
IP
IP
architecture is a design pattern used in React applications to manage the flow of data and state within the
on. It was developed by Facebook to address the problem of data flow in large and complex applications. I
ux promotes a unidirectional flow of data, which can make it easier to understand and maintain your code
w**: The user interface (UI) that the user interacts with. In a React application, this would be your compon
on**: Events or user interactions that trigger changes in the application's state. These are plain JavaScript
ribe what should happen.
atcher**: A central hub that receives actions and dispatches them to the appropriate stores. It ensures th
re processed in a predictable order.
e**: Stores contain the application's state and business logic. They listen to actions, update their state, an
s of changes. Each store is responsible for a specific part of the application's state.
w (again)**: Once the store updates, it notifies the views, which then re-render with the new data.
w**: You have a React component that displays the to-do items.
on**: When a user clicks a "Add New Task" button, an action is dispatched, e.g., `{ type: 'ADD_TASK', text:
' }`.
atcher**: The Dispatcher receives this action and sends it to the appropriate Store.
e**: You have a `TaskStore` that listens for the 'ADD_TASK' action. It updates its internal state to include t
`{ id: 1, text: 'Buy groceries', completed: false }`.
again): The `TaskStore` notifies the view that the state has changed. The React component displaying the t
rs with the updated list, now including the new task.This unidirectional flow of data ensures that your app
nges are predictable and maintainable. Actions are the only way to modify the state, and changes flow in
, which makes it easier to debug and reason about your application's behavior.
JAVASCRIPT PROMISES
JavaScript Promise are easy to manage when dealing with multiple asynchronous
operations where callbacks can create callback hell leading to unmanageable code. Prior to
promises events and callback functions were used but they had limited functionalities and
created unmanageable code. Multiple callback functions would create callback hell that
leads to unmanageable code. Promises are used to handle asynchronous operations in
JavaScript.
Syntax:
let promise = new Promise(function(resolve, reject){
//do something
});
Parameters
• The promise constructor takes only one argument which is a callback function
• The callback function takes two arguments, resolve and reject
• Perform operations inside the callback function and if everything went well then call
resolve.
• If desired operations do not go well then call reject.
A Promise has four states:
Promise Consumers: Promises can be consumed by registering functions using .then and
.catch methods.
1.Promise then() Method: It is invoked when a promise is either resolved or rejected. It
may also be defined as a carrier that takes data from promise and further executes it
successfully.
• The first function is executed if the promise is resolved and a result is received.
• The second function is executed if the promise is rejected and an error is received. (It
is optional and there is a better way to handle error using .catch() method
Syntax:
.then(function(result){
//handle success
}, function(error){
//handle error
})
Example 2: This example shows how the then method handles when a promise is resolved
• Javascript
let promise = new Promise(function (resolve, reject) {
resolve('Geeks For Geeks');
})
promise
.then(function (successMessage) {
//success handler function is invoked
console.log(successMessage);
}, function (errorMessage) {
console.log(errorMessage);
});
Loops:
Loop**: A definite loop is one where the number of iterations is known in advance. The `for` loop is typic
u have a specific number of iterations.
cript
i = 0; i < 5; i++) {
e to execute repeatedly
` Loop: This loop is used for iterating over the enumerable properties of an object. It's often used for obje
perties.
cript
key in object) {
ect.hasOwnProperty(key)) {
de to execute for each property
f` Loop The `for...of` loop is used for iterating over the values of iterable objects like arrays, strings, maps,
cript
nst value of iterable) {
e to execute for each value
nite Loops:**
` Loop: An indefinite loop is one where the number of iterations is not known in advance and depends on
n. The `while` loop keeps running as long as a condition is true.
cript
ondition) {
e to execute as long as the condition is true
hile` Loop: This loop is similar to the `while` loop but guarantees that the code block inside the loop will ru
e, as the condition is checked after the loop body execution.
cript
w function
functions are introduced in the ES6 version. Arrow functions enable us to write functions
with simpler and shorter syntax and make our code more readable and organized.
Arrow functions are anonymous functions i.e. they are functions without a name and are
not bound by an identifier. Arrow functions do not return any value and can be declared
without the function keyword. They are also called Lambda Functions.
• Arrow functions do not have the prototype property like this, arguments, or super.
• Arrow functions cannot be used with the new keyword.
• Arrow functions cannot be used as constructors.
Syntax:
Uses of arrow function: The arrow function is a single liner we needn’t use the return
keyword.
• JavaScript
// Arrow function for multiplying two numbers
value = (a, b) => a * b;
console.log(value(3, 5));
Output:
15
Example 2: When there are more than two lines to process
From the previous example, we can see that when there’s a single line of code to be
executed we didn’t use the return keyword but if there are more than two lines to be
processed, we need to use a return keyword. let’s demonstrate that with an example:
• JavaScript
number = (a, b) => {
c = 5;
return (a + b) * c;
};
console.log(number(2, 3));
Output:
25
Word.
ES^ vs Es5
It has a lower
It has a higher
5. performance as
performance than ES5.
compared to ES6.
Object manipulation is
Object manipulation is
6. less time-consuming in
time-consuming in ES5.
ES6.
An arrow function is a
In ES5, both function new feature introduced
and return keywords in ES6 by which we
7.
are used to define a don’t require the
function. function keyword to
define the function.
It provides a larger
It provides a less range
range of community
8. of community supports
supports than that of
than that of ES5
ES6
CHP1
XML vs JSON
JSON XML
Its files are very easy to read as Its documents are comparatively
compared to XML. difficult to read and interpret.
The Document Object Model is a programming interface for web documents. It represents
the page so that programs can change the document structure, style, and content. The
DOM represents the document as a tree of objects that can be easily manipulated using
scripting languages like JavaScript. It provides a structured representation of the webpage,
allowing you to interact with and modify its elements.
1. **`getElementById(id)`**:
- Example:
```javascript
```
2. **`getElementsByClassName(className)`**:
- Example:
```javascript
const elements = document.getElementsByClassName("myClass");
```
3. **`getElementsByTagName(tagName)`**:
- Example:
```javascript
```
4. **`querySelector(selector)`**:
- Returns the first element that matches the specified CSS selector.
- Example:
```javascript
```
5. **`querySelectorAll(selector)`**:
- Returns a NodeList of all elements that match the specified CSS selector.
- Example:
```javascript
8. **`addEventListener(event, callback)`**:
```javascript
button.addEventListener("click", () => {
alert("Button clicked!");
});
```
9. **`setAttribute(name, value)`**:
- Example:
```javascript
element.setAttribute("data-id", "123");
```
10. **`removeAttribute(name)`**:
- Example:
```javascript
element.removeAttribute("data-id");
```
11.onChange
12.onSubmit
12 onClick
REST API
Representational State Transfer (REST) is an architectural style that defines a set of constraints to be
used for creating web services. REST API is a way of accessing web services in a simple and flexible
way without having any processing.
REST technology is generally preferred to the more robust Simple Object Access Protocol (SOAP)
technology because REST uses less bandwidth, simple and flexible making it more suitable for
internet usage. It’s used to fetch or give some information from a web service. All communication
done via REST API uses only HTTP request.
Working: A request is sent from client to server in the form of a web URL as HTTP GET or POST or
PUT or DELETE request. After that, a response comes back from the server in the form of a resource
which can be anything like HTML, XML, Image, or JSON. But now JSON is the most popular format being
used in Web Services.