Web Development

Avoiding race conditions and memory leaks in React useEffect

Avoiding race conditions and memory leaks in React useEffect

  • February 8, 2021

Let us take a look at an implementation of getting data from an API request and see if there is any possibility of race conditions happening in this component: We have specified an empty array as a dependency to the useEffect React hook. So we have ensured that the fetch...

Object initialization shorthand notations in JavaScript

Object initialization shorthand notations in JavaScript

  • February 2, 2021

I was recently working on a project in which I was trying to use a shorthand notation for destructuring assignment of a variable. I was researching different ways of getting a specific scenario to work. And while doing that research, I found that ES2015 had added 3 new object initialization...

Using the useCallback React hook

Using the useCallback React hook

  • January 28, 2021

The useCallback React hook is a useful hook that can help in optimizing the rendering performance of our functional React components. It is used to memoize functions which means it caches the return value of a function given a set of input parameters. The syntax As we can see, the...

Understanding the useRef React hook

Understanding the useRef React hook

  • January 21, 2021

Continuing our React hooks series, we will learn about the useRef React hook in this blog post. The useRef React hook is useful in the following two situations: Before we see these advantages of the hook, let us first understand what the hook is and what it does. What is...

How to cancel an HTTP fetch request

How to cancel an HTTP fetch request

  • January 19, 2021

JavaScript promises have been a huge catalyst for asynchronous coding in the language. They have vastly improved the performance and experience for web development. One shortcoming of native promises has been that we were not able to cancel an HTTP fetch request once it was initiated. But now there is...

Use redux-like middleware for useReducer in React

Use redux-like middleware for useReducer in React

  • January 14, 2021

If you have used Redux before, you would be aware of the concept of middlewares. Now that useReducer has become a commonly used react hook, we might want to replicate the idea of middleware for the useReducer hook as well. If you do not know about middlewares, middlewares are functions...

How to write your own custom React hooks

How to write your own custom React hooks

  • January 12, 2021

If you have been using react for a while, chances are you have come across the need to extract some logic into a reusable function. And with React hooks coming into the picture, doing this has become a walk in the park. We can write our own custom react hooks...

Provide callback to useState hook like setState

Provide callback to useState hook like setState

  • December 15, 2020

If you have been writing class components for a while, you might be familiar with the callback functionality that the setState function provides. setState allows a second parameter to be passed to it as a callback. The callback function is invoked whenever the state of the function gets updated. But,...