Understanding Angular lifecycle methods with Examples
In Angular, components have a lifecycle managed by Angular itself, from creation to destruction. Angular provides lifecycle methods that you can use to tap into this process and perform custom logic at specific points in the lifecycle.
Here are some examples of how you might use each lifecycle method:
ngOnChanges
: This method is called whenever a bound input property changes. You can use it to react to changes in input properties and perform logic when the value of an input property changes.
import { Component, Input, OnChanges } from '@angular/core'; @Component({ selector: 'app-counter', template: '<p>{{counter}}</p>' }) export class CounterComponent implements OnChanges { @Input() counter: number; ngOnChanges(changes: SimpleChanges) { console.log(changes); } }
In this example, the CounterComponent
implements the ngOnChanges
method and logs the changes to its input properties.
ngOnInit
: This method is called after the firstngOnChanges
call and is used to perform initialization logic for the component.
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-hello',
template: '<p>Hello, {{name}}!</p>'
})
export class HelloComponent implements OnInit {
name: string;
ngOnInit() {
this.name = 'Angular';
}
}
In this example, the HelloComponent
implements the ngOnInit
method and initializes its name
property.
ngDoCheck
: This method is called during every Angular change detection cycle and is used to perform custom change detection.
import { Component, DoCheck } from '@angular/core';
@Component({
selector: 'app-todo-list',
template: `
<ul>
<li *ngFor="let item of items">{{item}}</li>
</ul>
`
})
export class TodoListComponent implements DoCheck {
items: string[];
ngDoCheck() {
console.log(this.items);
}
}
In this example, the TodoListComponent
implements the ngDoCheck
method and logs its items
array.
ngAfterContentInit
: This method is called after the component’s content has been fully initialized.
import { Component, AfterContentInit } from '@angular/core';
@Component({
selector: 'app-tabs',
template: `
<ng-content></ng-content>
`
})
export class TabsComponent implements AfterContentInit {
ngAfterContentInit() {
console.log('Tabs content initialized');
}
}
In this example, the TabsComponent
implements the ngAfterContentInit
method and logs a message to the console.
ngAfterContentChecked
: This method is called after every change detection cycle that updates the component’s content. It can be used to perform logic that depends on the component’s content being fully updated and checked.
import { Component, AfterContentChecked } from '@angular/core';
@Component({
selector: 'app-tabs',
template: `
<ng-content></ng-content>
`
})
export class TabsComponent implements AfterContentChecked {
ngAfterContentChecked() {
console.log('Tabs content checked');
}
}
In this example, the TabsComponent
implements the ngAfterContentChecked
method and logs a message to the console.
6. ngAfterViewInit
: This method is called after the component’s view (including its child views) has been fully initialized.
import { Component, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-tab-panel',
template: `
<ng-content></ng-content>
`
})
export class TabPanelComponent implements AfterViewInit {
ngAfterViewInit() {
console.log('Tab panel view initialized');
}
}
In this example, the TabPanelComponent
implements the ngAfterViewInit
method and logs a message to the console.
7. ngAfterViewChecked
: This method is called after every change detection cycle that updates the component’s view (including its child views).
import { Component, AfterViewChecked } from '@angular/core';
@Component({
selector: 'app-tab-list',
template: `
<ng-content></ng-content>
`
})
export class TabListComponent implements AfterViewChecked {
ngAfterViewChecked() {
console.log('Tab list view checked');
}
}
In this example, the TabListComponent
implements the ngAfterViewChecked
method and logs a message to the console.
8. ngOnDestroy
: This method is called just before the component is destroyed by Angular. It can be used to perform cleanup logic for the component, such as unsubscribing from observables or detaching event handlers.
import { Component, OnDestroy } from '@angular/core';
@Component({
selector: 'app-timer',
template: '<p>{{time}}</p>'
})
export class TimerComponent implements OnDestroy {
time: number;
intervalId: number;
ngOnInit() {
this.intervalId = setInterval(() => {
this.time = Date.now();
}, 1000);
}
ngOnDestroy() {
clearInterval(this.intervalId);
}
}
In this example, the TimerComponent
implements the ngOnDestroy
method and uses it to stop the interval timer it started in its ngOnInit
method.
I hope you found this article useful and informative. Thanks 😊
Creating a Vue.JS Instance Using its Fundamental Concepts
Vue.js is simple and powerful, and it is easy to learn. Once you understand the basics of the framework, everything will work in just the way you expect. The framework will help you to keep focused on writing the logic of your application instead of remembering a bunch of APIs that are hard to use. This article will help you learn some of the fundamental concepts in Vue.js.
Fundamental concepts
When you start a Vue application, what you need to keep in mind is your application’s logic itself. You don’t need to remember a set of APIs so that you can connect different pieces of your code. Vue.js, which is a progressive framework, provides you with an intuitive way of writing web applications, starting small and growing incrementally into a large-scale application. If you have used other frameworks before, you may wonder why they make things unnecessarily complicated. Now, let’s go through fundamental concepts in Vue.js and create a sample application. You can also access the complete code for this article at https://github.com/PacktPublishing/Building-Applications-with-Spring-5-and-Vue.js-2/tree/master/Chapter02/MessagesApp.
Creating a Vue instance
Creating a Vue
instance is the start of every Vue.js application. Typically, a Vue application
consists of two types of Vue instance—the root Vue instance and component
instances. You create the root instance with the Vue
function, as follows:
new Vue({/* options */});
The options
object here is where you describe
your application. Vue.js takes this object and initializes the Vue instance. Let’s
create a simple application, called the Messages App and see how to use
the options
object.
This SPA has the following features:
- Add a message
- View messages list
- Delete a message
- Automatically disable the add feature under certain conditions
We will start by creating the index.html
file and, from there; we will build our application incrementally. Let’s have a look at the index.html
file:
1.<!DOCTYPE html>
2.<html>
3.<head><title>Messages App</title></head>
4.<body>
5.
6.https://unpkg.com/vue@2.5.13/dist/vue.js
7.
8.let vm = new Vue({
9.el: '#app'
10. });
11.
12. </body>
13. </html>
In line Now, let’s define
our data model of the application. We need an array to hold those added
messages and a string to bind to the form’s input which will accept new
messages. Here is how the We add the Besides using a
plain object as the value of the Using a function
is required when you define the data structure for a component because, in that
way, Vue.js will always create a fresh data model for the new component. If you
use a plain object to define a component’s data model, all of the
instances of that component will share the same For now, we have
only defined the data model, and we haven’t told Vue.js what to do with
the The second way is to put the template directly inside the mounting point, In line In line In line In line In line Here is how the The However, you
should not use arrow functions syntax to create methods because you will lose
access to the Vue instance via Inside the Now, let’s add a way to delete a message from the UI. Here is what we change in the template: We add a button and use We delete the
selected message from the Now, let’s add the ability to automatically disable the What if we want
to change the logic so that the Here, we can use As you can see, the As you can see,
our template becomes much easier to follow and maintain since you keep most of
the logic in the JavaScript rather than in the HTML template. For the By now, we have implemented all of the features of the Messages App. Since we didn’t use Besides the By now, you’ve
learned how to use the Now, let’s
open the Apart from
the If you found this
article interesting, you can explore Building Applications with Spring 5 and
Vue.js 2 to Become efficient in
both frontend and backend web development with Spring and Vue. With the help of
Building Applications with Spring 5 and
Vue.js 2 you’ll get to grips with
Spring 5 and Vue.js 2 as you learn how to develop a web application. Node.js is an event-driven, server-side JavaScript environment. Node.js runs JS using the V8 engine developed by Google for use in their Chrome web browser. Leveraging V8 allows Node.js to provide a server-side runtime environment that compiles and executes JS at lightning speeds. This article covers the Node’s module system and the different categories of the Node.js modules. Like most programming languages, Node.js uses modules as a way of organizing code. The module system allows you to organize your code, hide information, and only expose the public interface of a component using module.exports. Node.js uses the CommonJS specification for its module system: Take a look at a simple example: To call other functions such as mul and div, use object destructuring as an alternative when requiring the module, for example, const { add } = require(‘./math’);. The code files for the section The Module System are placed at Code/Lesson-1/b-module-system. You can place Node.js modules into three categories: These are modules that can be used straight away without any further installation. All you need to do is to require them. There are quite a lot of them, but here are a few that you are likely to come across when building web applications: For example, the following code reads the content of the lesson-1/temp/sample.txt file using the in-built fs module: Node Package Manager (npm) is the package manager for JavaScript and the world’s largest software registry, enabling developers to discover packages of reusable code. To install an npm package, you only need to run the npm install <package-name> command within your project directory. Here’s a simple example. If you want to use a package (library) like request in your project, you can run the following command on your Terminal, within your project directory: To use it in your code, you should require it, like any other module: More details about npm can be found at https://docs.npmjs.com/. When you run the npm install <module-name> command on your project for the first time, the node_modules folder gets created at the root of your project. It’s worth noting how Node.js goes about resolving a particular required module. For example, if a /home/tony/projects/foo.js file has a require call require(‘bar’), Node.js scans the filesystem for node_modules in the following order. The first bar.js that is found is returned as follows: Node.js looks for node_moduels/bar in the current folder followed by every parent folder until it reaches the root of the filesystem tree for the current file. Note that the module foo/index.js can be required as foo, without specifying an index and will be picked by default. Now dive a little deeper into npm, by looking at some of the handy npm commands that you will often need: You have already looked at how local modules are loaded from the previous example that had math.js and index.js. Since JavaScript Object Notation (JSON) is such an important part of the web, Node.js has fully embraced it as a data format, even locally. You can load a JSON object from the local filesystem the same way you load a JavaScript module. During the module loading sequence, whenever a file.js is not found, Node.js looks for a file.json. See the example files in lesson-1/b-module-system/1-basics/load-json.js: Here, you will notice that once required, the JSON file is transformed into a JavaScript object implicitly. Other languages will have you read the file and perhaps use a different mechanism to convert the content into a data structure such as a map, a dictionary, and so on. For local files, the extension is optional, but should there be a conflict, it might be necessary to specify the extension. If you have both a sample.js and a sample.json file in the same folder, the .js file will be picked by default; it would be prudent to specify the extension, for example const config = require(‘./config/sample.json’). When you run npm install, without specifying the module to install, npm will install the list of packages specified (under dependencies and devDependencies in the package.json file in your project). If package.json does not exist, it will give an error indicating that no such file has been found. Open the IDE and the Terminal to implement this solution and learn how to write a basic Node.js file and run it. Write a very basic mathematical library with handy mathematical functions using the following steps: The 16 and 21 values should be printed out on the Terminal. This activity will build upon the, Running Basic Node.js activity. If the argument is a single array, sum up the numbers, and if it’s more than one array, first combine the arrays into one before summing up. Use the concat() function from lodash, which is a third-party package that you need to install. Now create a new function, sumArray, which can sum up numbers from one or more arrays using the following steps: Notice that you’re adding the –save option on your command so that the package installed can be tracked in package.json. When you open the package.json file created in step 3, you will see an added dependencies key with the details. You should see 21 and 30 printed out. If you found this article interesting, you can explore Anthony Nandaa’s Beginning API Development with Node.js to learn everything you need to get up and running with cutting-edge API development using JavaScript and Node.js. Beginning API Development with Node.js begins with the basics of Node.js in the context of backend development and quickly leads you through the creation of an example client that pairs up with a fully authenticated API implementation. React is mainly written with modern JavaScript (ES6, ES7, and ES8). If you want to take advantage of React, there are some modern JS features that you should master to get the best results for your React applications. In this article, you’ll learn the essential JS features so that you are ready to start working on your first React application. In this section, you’ll see how to use the most important JS features in React: If you found this article interesting, you can explore React Cookbook, which covers UI development, animations, component architecture, routing, databases, testing, and debugging with React. React Cookbook will save you from a lot of trial and error and developmental headaches, and you’ll be on the road to becoming a React expert. A queue is a programming construct that bears a heavy resemblance to real-world queues, for example, a queue at the movie theater, ATMs, or the bank. Queues, as opposed to stacks, are first-in-first-out (FIFO), so whatever goes in first comes out first as well. This is especially helpful when you would like to maintain data in the same sequence in which it flows. Before you understand queues, take a quick look at the types of queues that you may want to use in your applications: Implementing an API is never as easy as it seems. When making generic classes, you can never predict what kind of situation your queue is going to be used in. Some of the most common operations that you can add to the queue are as follows: Of the four types of queues discussed earlier, this article will teach you to implement a simple and priority queue. To create a queue, use the following steps: You need to wrap the entire class inside an IIFE because you don’t want to make Queue items accessible from the outside: To test this queue, you can simply instantiate it and add/remove some items to/from the queue: As you can see in above code, all the elements are treated the same. Irrespective of the data they contain, elements are always treated in a FIFO fashion. Although that is a good approach, sometimes you may need something more: the ability to prioritize elements that are coming in and leaving the queue. A priority queue is operationally similar to a simple queue, that is, they support the same API, but there is a small addition to the data they hold. Along with the element (your data), they can also persist a priority, which is just a numerical value indicating the priority of your element in the queue. Addition or removal of these elements from the queue is based on priority. You can either have a minimum priority queue or a maximum priority queue, to help establish whether you are adding elements based on increasing priority or decreasing priority. Now let’s see how we can use the add() method in the simple queue: Since you are accounting for the priority of the elements while they are being inserted into the stack, you do not have to concern yourself with priority while you remove elements from the queue. So, the remove() method is the same for both simple and priority queues. Other utility methods, such as front(), clear(), peek(), and size(), have no correlation with the type of data that is being saved in the queue, so they remain unchanged as well. A smart move while creating a priority queue would be to optimize your code and decide whether you would like to determine the priority at the time of addition or removal. That way, you are not over calculating or analyzing your dataset at each step. First set up the data for testing the queue: Visually, the preceding steps would generate a queue that looks as follows: Note how when you add an element with a priority 2 it gets placed ahead of all the elements with priority 1: And when you add an element with priority 1 (lowest), it gets added to the end of the queue: In the above image, by adding the last element apply the lowest priority in the order, which makes it the last element of the queue, thus keeping all the elements ordered based on priority. Now, remove the elements from the queue: There you have it: the creation of simple and priority queue in JavaScript using WeakMap(). If you found this article interesting, you can explore Kashyap Mukkamala’s Hands-On Data Structures and Algorithms with JavaScript to increase your productivity by implementing complex data structures and algorithms. This book will help you gain the skills and expertise necessary to create and employ various data structures in a way that is demanded by your project or use case.5
, we create a app
id in the DOM. And in line 9
, we mount our application to this element
by using the el
property of
the options
object. el
is short for element, and its value can be a CSS selector
string, like the one we use here, '#app'
, or it can be the HTMLElement
itself, document.getElementById('app')
. In line 8
, we assign the Vue instance to the vm
variable, which is short for ViewModel
.
data
object appears:...
let vm = new Vue({
el: '#app',
data: {
messages: [],
newMessage: ''
}
});
...
data
object using object literals. As you
can see, it is quite straightforward. We give them initial values so that you
can easily tell that messages
is an array and newMessage
is a string. Providing initial
values for the data
object properties is a good practice. It not
only helps you understand the data model better, but also makes those
properties reactive by default. data
property of the options
object, you can also use a function
that returns a plain object, as in the example:...
data () {
return {
messages: [],
newMessage: ''
}
}
...
data
object, which is not desired. For
our root Vue instance here, we are safe to use a plain object.data
object.
Let’s add a template for displaying and adding messages. You can add a template
in three ways. One is to add an inline template string using the template
property of the options
object. It is appropriate to adopt
this approach when you create a component that doesn’t have a lot of markups. . Vue.js will parse the template inside
#app
and replace it with HTML generated by Vue.js. The third way is to put the template markup inside a script
tag, for example, <script type="x-template" id="tmplApp">
, and put '#tmplApp'
as the value of the template
property of the options
object. We will adopt the second approach here just so we can have the template markup close to the final output. Here is how the template appears:...
5.
7.
11.
12.
14.
15. </form>
16. </div>
...
7
, we use the Vue built-inv-for
directive to render
the messages list. The syntax of the v-for
directive is alias in source
. In our code, message
is alias
and messages
is source
. We don’t need to write vm.messages
in order to access the messages
property. Just use the exact name
that you put in the data
object. And by adding the v-for
directive to the li
tag, we create a v-for
block inside the li
tag, and that’s where thealias message
will be available. You can think of
the v-for
block
as being equivalent to the for-loop
block in JavaScript.8
, we use Mustache syntax to output the text
property and createdAt
property of a message
object of the messages
list. The createdAt
property is a Date
object that we add when saving a new
message. When Vue.js parses the template and interpolates a Mustache tag, for
example, {{message.text}}
,
it creates data binding between the output and the data. It will replace the
tag with the actual value and update the output whenever the text
property has been changed. The text
interpolation also supports JavaScript expression. For example, you can make
the text
property
always in lower case with {{message.text.toLowerCase()}}
.11
, we use another built-in directive, v-on
, to attach an event listener to the
form’s submit
event.prevent
is a modifier, telling Vue.js to
call event.preventDefault()
so that the browser won’t actually submit the form. addMessage
is a method that will be invoked
when the form’s submit
event is triggered. We will create this method
shortly. You can use v-on
to attach listeners to all of the normal DOM
events, for example, click
and mouseover
. You can also use it to listen to custom events
of Vue’s custom components in the same way. We will see how that works shortly.12
, we use the built-inv-model
directive to create a two-way
binding between the textarea
element and newMessage
property of the data
object. In this way, whenever the
value of the textarea
element is changed, the newMessage
will be updated automatically. And
when newMessage
is
changed, textarea
will
be updated accordingly. This is such a nice feature that you can get the value
of the textarea
element
without touching it yourself specifically. It works just as you would
imagine it should.14
, we add a button with type="submit"
to trigger the submit
event of the form. Now, let’s create
our addMessage
method
to listen to that event. We can do it by using the methods
property of the options
object.options
object appears with the addMessage
method:...
let vm = new Vue({
...
data: {
...
},
methods: {
addMessage (event) {
if (!this.newMessage) {return;}
this.messages.push({
text: this.newMessage, createdAt: new Date()});
this.newMessage = '';
}
}
});
...
methods
property of the options
object takes an object, where you put all of your methods. And inside these methods, you have access to the properties of the data
object via this
, as you can see that we use this.newMessage
and this.messages
inside the addMessage
method to access them. The method syntax we use here is ES6, but you can also use function expression, as follows:addMessage: function (event) {
// Logic goes here
}
this
.addMessage
method, we add the new message to
the messages
array
using the push()
method,
and then we reset the newMessage
property. Accordingly, Vue.js will clear textarea
in the UI automatically. This
is the magic of two-way binding, which will be revealed soon....
<li v-for="message in messages">
{{ message.text }} - {{ message.createdAt }}
<button @click="deleteMessage(message)">X</button>
</li>
...
@click
, the short-hand of v-on:click
, to attach the listener deleteMessage
method to the click
event. Instead of putting the method’s name here, we use an inline statement to pass the message
object to the method. And here are the updated methods
of the options
object:...
let vm = new Vue({
...
methods: {
...
deleteMessage (message) {
this.messages.splice(this.messages.indexOf(message), 1)
}
}
});
...
messages
array using the Array.prototype.splice()
method. Vue.js will detect this
change and update the DOM automatically. You don’t need to manipulate the DOM
at all.add
feature. Let’s say we want to disable the Add
button when there are 10
messages in the list. To do that, we can use the built-in v-bind
directive to bind the Add
button’s disabled
attribute with the messages.length >= 10
expression. In this way, Vue.js will update the disabled
attribute automatically when the length of the messages
array changes. Here is the updated template:...
<form @submit.prevent="addMessage">
...
Add
button is disabled when the length of
the textarea
input
exceeds 50 characters? You will need to change the value of the v-bind
directive to newMessage.length
> 50
. What if we
want to disable the button when there are already 10
messages, or the length of newMessage
exceeds 50 characters? We can change the
directive value to messages.length >= 10 ||
newMessage.length > 50
. It still works. However, as you can see, the code starts to bloat and it
would become hard to maintain when you need to add more logic to
decide when the Add
button should be disabled.computed
properties. As the name suggests, the value of such a property is computed rather than defined as those in the data
object. And Vue.js will track the dependencies of a computed
property and update the property’s value when the dependencies change. Let’s add the computed
property addDisabled
to the options
object:let vm = new Vue({
data {
...
},
computed: {
addDisabled () {
return this.messages.length >= 10 || this.newMessage.length > 50;
}
},
...
});
addDisabled
computed property is defined as a method of the computed
object of the options
object. Inside the method, you also have access to the Vue instance via this
. For the v-bind
directive, there is also a shorthand option, which is a colon (:
). Let’s update the Add
button in the template to the following:<button :disabled="addDisabled" type="submit">Add</button>
v-bind
directive, you can use
it to bind the HTML element’s built-in attributes, for example, class
and style
. You can also use it to bind a Vue’s custom
component property. We will see how that works shortly.<script type="module">
, you can open index.html
directly using Chrome. If you try it now, you will see something strange. Immediately after opening the file, you can see the template markups that we put inside the mounting point, v-cloak
directive to the mounting point and inserting a CSS rule to hide the template markups. Vue.js will remove the v-clock
directive when the generated DOM is ready. The following are the updates to the index.html
file:
...
[v-cloak] {display: none;}
body > div {width: 500px; margin: 0 auto;}
textarea {width: 100%;}
ul {padding: 0 15px;}
[v-cloak]
CSS rule, we add a few other rules
to style the UI a little bit, even though it is still very primitive with these
rules. Now, if you open it again in the browser, there is no flash anymore. data
object, the computed
object, and the methods
object of the options
object of a Vue instance. And you
can see that, even though the properties of these objects are defined
separately, you can access them in the same way, which is via this
. Console
tab
of Chrome’s Developer tools. Instead of using the input field in the UI, let’s
add a new message from the console by interacting directly with
the vm
object,
which is the root Vue instance that we created and made available in the global
scope. As you can see from the following screenshot, it works as you would
expect. This is its simplicity and powerfulness:data
, computed
, and methods
properties, the options
object has many other properties
that you can use to define a Vue instance. Using Modules in Node.js
The Module System
Application Modularization
// math.js file
function add(a, b)
{
return a + b;
}
…
…
module.exports =
{
add,
mul,
div,
};
// index.js file
const math = require('./math');
console.log(math.add(30, 20)); // 50
Module Categories
Built-In Modules
const fs = require('fs');
let file = `${__dirname}/temp/sample.txt`;
fs.readFile(file, 'utf8', (err, data) =>
{
if (err) throw err;
console.log(data);
});
npm – Third-Party Module Registry
npm install request
const request = require('request');
request('http://www.example.com', (error, response, body) =>
{
if (error) console.log('error:', error); // Print the error if one occurred
else console.log('body:', body); // Print the HTML for the site.
});
Scanning for node_modules
Handy npm Commands
Local Modules
const config = require('./config/sample');
console.log(config.foo); // bar
Activity: Running Basic Node.js Code
mkdir -p beginning-nodejs/lesson-1/activity-a
console.log(add(10, 6)); // 16
console.log(sum(10, 5, 6)); // 21
node activity-a/math.js
Activity: Using a Third-Party Package
npm init
npm install lodash--save
const _ = require('lodash');
function sumArray()
{
let arr = arguments[0];
if (arguments.length > 1)
{
arr = _.concat(...arguments);
}
// reusing the sum function
// using the spread operator (...) since
// sum takes an argument of numbers
return sum(...arr);
}
// testing
console.log(math.sumArray([10, 5, 6])); // 21
console.log(math.sumArray([10, 5], [5, 6], [1, 3])) // 30
node index.js
How to Work with the Latest JS features in React
Working with the latest javascript features in React
How to do it
var name = 'Carlos Santana';
let age = 30;
console.log(window.name); // Carlos Santana
console.log(window.age); // undefined
for (var i = 1 ; i <= 10; i++) {
console.log(i); // 1, 2, 3, 4... 10
}
console.log(i); // Will print the last value of i: 10
for (let i = 1 ; i <= 10; i++) {
console.log(i); // 1, 2, 3, 4... 10
}
console.log(i); // Uncaught ReferenceError: i is not defined
const pi = 3.1416;
pi = 5; // Uncaught TypeError: Assignment to constant variable.
constcryptoCurrencies = ['BTC', 'ETH', 'XRP'];
// Adding ERT: ['BTC', 'ETH', 'XRP', 'ERT'];
cryptoCurrencies.push('ERT');
// Will remove the first element: ['ETH', 'XRP', 'ERT'];
cryptoCurrencies.shift();
// Modifying an element
cryptoCurrencies[1] = 'LTC'; // ['ETH', 'LTC', 'ERT'];
const person = {
name: 'Carlos Santana',
age: 30,
email: 'carlos@milkzoft.com'
};
// Adding a new node...
person.website = 'https://www.codejobs.com';
// Removing a node...
deleteperson.email;
// Updating a node...
person.age = 29;
this.setState({
items: [
...this.state.items, // Here we are spreading the current items
{
task: 'My new task', // This will be a new task in our Todo list.
}
]
});
render() {
const props = {};
props.name = 'Carlos Santana';
props.age = 30;
props.email = 'carlos@milkzoft.com';
return<Person{...props}/>;
}
functionsetNumbers(param1, param2, ...args) {
// param1 = 1
// param2 = 2
// args = [3, 4, 5, 6];
console.log(param1, param2, ...args); // Log: 1, 2, 3, 4, 5, 6
}
setNumbers(1, 2, 3, 4, 5, 6);
// Imagine we are on our <Person> component and we are
// receiving the props (in this.props): name, age and email.
render() {
// Our props are:
// { name: 'Carlos Santana', age: 30, email:
'carlos@milkzoft.com' }
console.log(this.props);
const{ name, age, email } = this.props;
// Now we can use the nodes as constants...
console.log(name, age, email);
return (
<ul>
<li>Name: {name}</li>
<li>Age: {age}</li>
<li>Email: {email}</li>
</ul>
);
}
// Also the destructuring can be used on function parameters
const Person = ({ name, age, email }) => (
<ul>
<li>Name: {name}</li>
<li>Age: {age}</li>
<li>Email: {email}</li>
</ul>
);
class Person extends Component {
showProps = () => {
console.log(this.props); // { name, age, email... }
}
render() {
return (
// Consoling props:
{this.showProps()}
); } }
render() {
const { theme } = this.props;
return (
<div>Some content here...</div>
); }
render() {
const tasks = [
{ task: 'Task 1' },
{ task: 'Task 2' },
{ task: 'Task 3' }
];
return (
<ul>
{tasks.map((item, key) =><likey={key}>{item.task}</li>}
</ul>
);
}
export default functioncoinsReducer(state = initialState, action) {
switch (action.type) {
caseFETCH_COINS_SUCCESS: {
const { payload: coins } = action;
returnObject.assign({}, state, {
coins
});
}
default:
return state;
}
};
import React, { Component } from 'react';
class Home extends Component {
render() {
return<h1>I'm Home Component</h1>;
}
}
export default Home;
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import logo from '../../images/logo.svg';
class Header extends Component {
staticpropTypes = {
title: PropTypes.string.isRequired,
url: PropTypes.string
};
render() {
const {
title = 'Welcome to React',
url = 'http://localhost:3000'
} = this.props;
return (
<header className="App-header">
<a href={url}>
<imgsrc={logo}className="App-logo"alt="logo"/>
</a>
<h1 className="App-title">{title}</h1>
</header>
);
}
}
export default Header;
Index.getInitialProps = async () => {
consturl = 'https://api.coinmarketcap.com/v1/ticker/';
const res = awaitaxios.get(url);
return {
coins: res.data
};
};
Creating Queues for In-Order Executions With JavaScript by using WeakMap()
Types of queues
Implementing APIs
Creating a queue
A simple queue
class Queue {
constructor() {
}
}
const qKey = {};
const items = new WeakMap();
class Queue {
constructor() {
}
}
var Queue = (() => {
const qKey = {};
const items = new WeakMap();
class Queue {
constructor() {
items.set(qKey, []);
}
add(element) {
let queue = items.get(qKey);
queue.push(element);
}
remove() {
let queue = items.get(qKey);
return queue.shift();
}
peek() {
let queue = items.get(qKey);
return queue[queue.length - 1];
}
front() {
let queue = items.get(qKey);
return queue[0];
}
clear() {
items.set(qKey, []);
}
size() {
return items.get(qKey).length;
}
}
return Queue;
})();
Testing a simple queue
var simpleQueue = new Queue();
simpleQueue.add(10);
simpleQueue.add(20);
console.log(simpleQueue.items); // prints undefined
console.log(simpleQueue.size()); // prints 2
console.log(simpleQueue.remove()); // prints 10
console.log(simpleQueue.size()); // prints 1
simpleQueue.clear();
console.log(simpleQueue.size()); // prints 0
Priority Queue
add(newEl) {
let queue = items.get(pqkey);
let newElPosition = queue.length;
if(!queue.length) {
queue.push(newEl);
return;
}
for (let [i,v] of queue.entries()) {
if(newEl.priority > v.priority) {
newElPosition = i;
break;
}
}
queue.splice(newElPosition, 0, newEl);
}
Testing a priority queue
var priorityQueue = new PriorityQueue();
priorityQueue.add({ el : 1, priority: 1});
// state of Queue
// [1]
// ^
priorityQueue.add({ el : 2, priority: 2});
// state of Queue
// [2, 1]
// ^
priorityQueue.add({ el : 3, priority: 3});
// state of Queue
// [3, 2, 1]
// ^
priorityQueue.add({ el : 4, priority: 3});
// state of Queue
// [3, 4, 2, 1]
// ^
priorityQueue.add({ el : 5, priority: 2});
// state of Queue
// [3, 4, 2, 5, 1]
// ^
priorityQueue.add({ el : 6, priority: 1});
// state of Queue
// [3, 4, 2, 5, 1, 6]
// ^
console.log(priorityQueue.remove());
// prints { el: 3, priority: 3}
// state of Queue
// [4, 2, 5, 1, 6]
console.log(priorityQueue.remove());
// prints { el: 4, priority: 3 }
// state of Queue
// [2, 5, 1, 6]
console.log(priorityQueue.remove());
// prints { el: 2, priority: 2 }
// state of Queue
// [5, 1, 6]
priorityQueue.print();
// prints { el: 5, priority: 2 } { el: 1, priority: 1 } { el: 6, priority: 1 }