How to use Context and useReducer for state management in React app? Example Tutorial

Introduction
A React component is made up of props and state. The state can be local or global. Usually, the local state is handled within the component using the setState method in class components and hooks in the functional components. But as the application grows, the state is usually required to be shared among multiple components. The global state is managed using third-party libraries such as Redux

Redux works perfectly but setting up redux is complicated and working with it is more complicated. So for moderate global state management, we can use Context API or the useReducer hook. But, there is one more way for managing the global state and that is by using the Context API and useReducer together.

What is "render props" in React.js? Example Tutorial

Props in React are properties that are passed from a parent component to a child component. Basically, props are plain JavaScript objects. Unlike the state, which is managed within the component, props are received as input from another component. As mentioned, props are JavaScript objects. But props are not limited to this only. There is another way of using props and it is known as “render props”. In this article, we will discuss what “render props” are and how to use this technique in React with the help of examples.

Top 20 Hibernate Interview Questions with Answers for Java Programmers

Hibernate is one of the most popular persistent frameworks in the Java world. Hibernate offers an object to relational (ORM) solution which frees Java developers from writing tedious, hard to read, and cluttered JDBC code converting SQL columns into Object properties. Apart from freeing Java developers from writing JDBC and database interaction code, Hibernate also offers the out-of-box solution on caching, proxying, and lazy loading which drastically improves the performance of your Java Web application. 

How to Parse JSON in Java Object using Jackson - Example Tutorial

Hello guys, if you are wondering how to parse JSON in Java then don't worry, there are many options. In the last article, I have shown you 3 ways to parse JSON in Java in this example, You will learn how to parse a  JSON String to Java and how to convert Java Object to JSON format using Jackson. JSON stands for JavaScript Object notation is a subset of JavaScript object syntax, which allows all JavaScript clients to process it without using any external library. Because of its compact size, compared to XML and platform independence nature makes JSON a favorite format for transferring data via HTTP. 

How to use useReducer in React.js and JavaScript? Example Tutorial

Introduction

Before the React hooks were introduced, it was not possible to use state in functional components. The React hooks transformed stateless functional components into stateful components in which state and lifecycle methods like functionality could be used. 


The useState hook is the primary hook for declaring the state in a functional component. Using it, a state variable can be declared and initialized along with a function to manipulate it. But sometimes state gets complex and the useState hook is not efficient to handle it.


So React provides another hook for complex state management in functional components. This hook is called useReducer. In this article, we will discuss what useReducer hook is and how to use it. 

7 Examples of HttpURLConnection in Java - Sending GET and POST Request [Tutorial]

If you want to learn how to send GET and POST requests from the Java program then you have come to the right place. Earlier, I have shared free Java Courses for beginners, and today, I am going to show you how to use HttpURLConnection class in Java to send HTTP requests to the server.  The HttpURLConnection is an important class in the java.net package which allows you to send an HTTP request from a Java program. By using this class you can send any kind of HTTP request like GET, POST, PUT, DELETE, HEAD, etc to the server and call REST APIs. 

3 Ways to change Embedded Tomcat Server Port in Spring Boot Application - Example

If you know Spring Boot really simplifies the Java web development by embedding essential libraries as well as a tomcat server to run Java web applications. By default, this tomcat server listens on port 4588, and for any reason, if you want to change, then Spring boot provides several configuration options to achieve that. For example, I wanted tomcat to listen on port 8080 because port 80 is forwarded to 8080 in our Linux machine, and we wanted our clients to provide our client with a clean URL without a port.

Difference between @SpringBootApplication vs @EnableAutoConfiguration annotations in Spring Boot? Example

Even though both @SpringBootApplication and @EnableAutoConfiguration can be used to enable the auto-configuration feature of Spring Boot, there is a subtle difference between them. The @SpringBootApplication does much more than what @EnableAutoConfiguration does. It's actually a combination of three annotations: @Configuration, which is used in Java-based configuration on Spring framework, @ComponentScan to enable component scanning of components you write like @Controller classes, and @EnableAutoConfgiuration itself, which is used to allow for auto-configuration in Spring Boot application.

Decorator Design Pattern Example in Java [Tutorial]

The Decorator design pattern is one of the famous Gang of Four (GOF) structural design patterns, which provides a dynamic way of extending an object's functionality. It's different than the traditional way of adding new functionality into an object using Inheritance, instead, it uses Composition which makes it flexible and allows the addition of new functionalities at the run time, as opposite to Inheritance, which adds new functionality at compile time. Because of this flexibility, Decorator is one of the darling patterns for many Java developers. 

Difference between notify and notifyAll in Java? [Answered]

wait, notify, and notifyAll methods are used for inter-thread communication in Java. wait() allows a thread to check for a condition, and wait if the condition doesn't meet, while notifying() and notifyAll() method informs waiting for a thread for rechecking condition, after changing the state of a shared variable. One good example of how to wait and notify method works is the Producer consumer problem, where one thread produces and waits if the bucket is full; and another thread consumes and waits if the bucket is empty. 

How to create a dynamic list in React? Example Tutorial

1. Introduction

Working with lists is common in React web development. Creating a static list is very easy but making it dynamic can be tough. Dynamic lists can generally be manipulated in one way or another. For example, a new item can be added to a list by entering a value in an input field or an item can be removed when it is clicked. 


In this tutorial, we will learn how to create a dynamic list in which a new item can be added through an input field. Moreover, we will learn how to delete an item when clicked.

How to Solve Producer Consumer Problem in Java using BlockingQueue [Example]

The Producer-Consumer problem is one of the classic multi-threading problems in computer science and the multi-threading world. It's tricky because it involves inter-thread communication, but it's important because most of the multi-threading problems fit into this category. Because of its importance, it's also known as Producer Consumer design patterns. There are many ways to solve the producer-consumer problem in Java, like you can solve this by using the wait() and notify() method, as discussed here, or you can use the Semaphore to solve this problem. 

Difference between CountDownLatch vs CyclicBarrier in Java Multithreading

Difference between CountDownLatch and CyclicBarrier in Java
Both CyclicBarrier and CountDownLatch are used to implement a scenario where one Thread waits for one or more Thread to complete their job before starting processing but there is one difference between CountDownLatch and CyclicBarrier in Java which separates them apart and that is, you can not reuse the same CountDownLatch instance once count reaches to zero and latch is open, on the other hand, CyclicBarrier can be reused by resetting Barrier, Once the barrier is broken.

Java 8 - Map and Reduce Example Tutorial

Hello folks, the map-reduce concept is one of the powerful concepts in computer programming, particularly on functional programming which utilizes the power of distributed and parallel processing to solve a big and heavy problem in a quick time. From Java 8 onwards, Java also got this powerful feature from the functional programming world. Many of the services provided on the internet like Google Search are based on the concept of the map and reduce. In map-reduce, a job is usually split from the input data-set into independent chunks which are processed by the map tasks in a completely parallel manner. The framework then sorts the outputs of the map operation, which are then supplied to the reduce tasks.

Top 5 Free & Paid Spring Certification Courses and Practice Tests

Many Java developers don't know that, similar to Oracle's Java certification, there is also a Spring certification program, which certifies yourself for your Spring framework skill. There are three Spring certifications currently available, the Spring Professional certification exam, also known as the Vmware Certified Spring Professional exam (VMware EDU-1202) which certifies on your general knowledge about Spring framework, Spring MVC skill, Spring Boot, Spring Data JPA, Testing with Spring and Spring Boot, and other advanced Spring skills. Earlier, SpringSource provides these certifications, but now they are provided by Vmware, the company behind the Spring framework.

What is objects in JavaScript? Example Tutorial

JavaScript is a dynamically typed programming language. In JavaScript, it is not required to define the data type of a variable but that does not mean JavaScript does not have data types. Data types in JavaScript are divided into two categories - primitive and non-primitive.


Primitive data types such as numbers and strings can hold only a single value. But non-primitive types can hold more than one value. Objects in JavaScript are non-primitive. 


If you have ever done programming, you may be familiar with the concept of objects because objects are one of the most important parts of programming. The reason is simple. Objects can hold multiple values. In this article, we will discuss objects in JavaScript. 

5 Best books to Learn JDBC for Java Programmers

The JDBC (Java Database connectivity) is one of the vital API in the Java programming language which allows a Java program to connect to any database, like Oracle, Microsoft SQL Server, MySQL, PostgreSQL, SQL Lite, Sybase or any other relational database. It's an essential API to learn and master for both core Java and Java EE professionals, given the ubiquitous nature of Databases in real-world applications. Despite its importance, many Java developer lacks essential JDBC skills, like they are not familiar with Connection, Statement, connection pool, calling stored procedures, and executing a transaction in JDBC.

What are prototypes in JavaScript? Example Tutorial

Introduction

If you have used JavaScript, then you are definitely familiar with objects because objects are one of the commonly used data types not only in JavaScript but also in the entire programming community. An object in JavaScript looks like the following.


{

    name: “Tom”,

    age: 28,

    city: “New York

}

 


So basically, an object stores data in key-value pairs. We can create an object like we did above, and then assign it to a variable. This is one of creating objects in JavaScript. Another way is by using the constructor function. 


Every object created in JavaScript has a default property named prototype. The prototype property is often a consulting concept in JavaScript. In this article, we will discuss what the prototype property is in JavaScript and how to use it. 





Prototype

Observe the following code.


function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

 



“Employee” is a constructor function. Let’s see what is the value of the “Employee. prototype”.

What are prototypes in JavaScript? Example Tutorial




The prototype has several methods and properties. Observe the following image. 


What are prototypes in JavaScript



So, basically, the “Employee.prototype.constructor” is referring to “Employee” only. If we console “Employee.prototype”, then it will return only an empty object. This happens because as of now, we haven't added anything in the prototype of the “Employee” constructor. 






Adding properties

The prototype can be used to add new properties to a defined constructor function. 


Let’s create an object first. 


function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

const Emp1 = new Employee("Tom", 28, "New York");


The “Emp1” has three properties as of now - name, age, and city. 



Now, suppose, we want to add a new property named gender to the “Employee”. We can do this using the prototype.


Employee.prototype.gender = "male";


Now, let’s see if there is any change in “Employee”.



No, because we have added “gender” to the prototype. 



Now observe the “Emp1” object we created earlier.



The property added using the prototype is already available in the “Emp1” object.



Let’s create another object. 


function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

const Emp1 = new Employee("Tom", 28, "New York");

 

Employee.prototype.gender = "male";

 

const Emp2 = new Employee("Lisa", 21, "Chicago”);


The problem here is that the value of “gender” is “Emp2” is also “male”. So to change, we can simply assign a new value to it. 



This way of adding new properties to a constructor function using the prototype and then inheriting them in the objects is known as prototype inheritance.


One important point here to remember is that if we change the value of “gender”, then it will be changed for every new object but not for those which are declared before it.

function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

Employee.prototype.gender = "male";

 

const Emp1 = new Employee("Tom", 28, "New York");

const Emp2 = new Employee("Jack", 25, "Detroit");

 

Employee.prototype = {

  gender: "female",

};

 

const Emp3 = new Employee("Lisa", 21, "Chicago");


Let’s check the value of “gender” for every object. 



The value for “Emp3” is changed but not for “Emp1” and “Emp2” because they were declared before changing the value of “gender”.






Adding methods

Similarly, we can also use the prototype property to add new methods to a constructor function. 


Observe the following code. 


Employee.prototype.getDetails = function () {

  console.log(

    `Name of the employee: ${this.name}. \nAge: ${this.age} \nCity: ${this.city} \nGender: ${this.gender}`

  );

};


Using the prototype property, the “getDetails” method is added to the “Employee”. 


Let’s use the objects to invoke the “getDetails” method. 



It will work similarly for the other object created using “Employee”.


Now observe the following code.


Employee.prototype.getDetails = () => {

  console.log(

    `Name of the employee: ${this.name} \nAge: ${this.age} \nCity: ${this.city} \nGender: ${this.gender}`

  );

};


The “getDetails” method is an arrow function. So this means, this method does not “this” keyword, meaning, it will return undefined for name, age, city, and gender. 


Wrapping it up

So the object prototype is a simple and powerful yet confusing concept in JavaScript. As objects are an important part of modern JavaScript, it is recommended to understand the concept of prototypes. In this article, we discussed what is prototype and how it is used to add properties and methods to a constructor function.