A comparison of the two libraries for capturing React components as images

Charts on a laptop screen
Charts on a laptop screen
Photo by Luke Chesser on Unsplash.

I recently wrote an article about capturing React components as images and shared it on Reddit. One redditor commented on it, saying that while the library is robust, they used another one that they thought performed a bit better:

“A similar library that I ultimately ended up using was html-to-image. Much much faster and not only converts components to canvas but directly to png/blob/svg.”

We use html2canvas in another part of our application where we need to download multiple visualizations as separate images, zip them, and then download them. It works fine, but I have to say the performance can…


Quick and easy randomization

Dice
Dice
Photo by Edge2Edge Media on Unsplash.

There was a recent feature request at work that called for picking a random number to represent an index for an array of objects. We needed this logic to create a new, smaller collection of randomly selected items from the original array.

In this article, I’ll talk about how to use Math.random() to generate a random number between two numbers.

“The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.” …


One sort function, two sort orders

I was recently working on a way to sort an array of objects alphabetically based on two columns. This means that both levels of alphabetical sorting should be maintained. See the example below.

var fruits = [
{colA: 'apple', colB: 'red delicious'},
{colA: 'pear', colB: 'red anjou'},
{colA: 'orange', colB: 'navel'},
{colA: 'orange', colB: 'blood'},
{colA: 'pear', colB: 'green anjou'},
{colA: 'apple', colB: 'granny smith'},
{colA: 'orange', colB: 'clementine'}
]

The above list is supposed to be sorted to look like the below array.

[
{colA: "apple", colB: "granny smith"},
{colA: "apple", colB: "red delicious"},
{colA: "orange"…

Just because you got an image in a certain color doesn’t mean it has to stay that way

Ladybug on a leaf
Ladybug on a leaf
Original photo (no BW) by Alexandre Debiève on Unsplash.

Imagine you receive a specific image from the product team that is meant to act as an icon for a button. The button has a white background, black border, black text, and the image itself is darker.


The “depends” Amazon JavaScript code challenge

Phone displaying Amazon logo
Phone displaying Amazon logo
Photo by Christian Wiediger on Unsplash.

I recently went through my old dev projects and came across a JavaScript code challenge I did for Amazon. I had about half an hour to do it, and while I couldn’t complete it within that time frame back then, I decided to give it another try.

The challenge required a function that takes an array of task objects, and based on the depends property in each object, evaluates each task. Below is the original array:

For this case, the function needs to loop through and log Task #n is complete where n is the object’s id. …


A coding challenge to clear up 3 regular expression concepts

White frame
White frame
Photo by Clark Van Der Beken on Unsplash.

I recently came across an interesting code challenge that consisted of a few questions. The first question can be seen below:

“We love word clouds! Given the following text, print a list of all the words used and the number of times they appear. Please ignore punctuation and other uninteresting characters. Words should not be case-sensitive.”

We can break this challenge down into a few parts.

1. Remove All Punctuation and “Uninteresting” Characters

We can accomplish this by implementing a quick regular expression:

text.replace(/[^A-Za-z]+/g, " ")

The code above is more effective because you don’t have to have a separate array containing all punctuation marks. This regex…


Photo by Jon Tyson on Unsplash

Until February of this year I had been living at home with my mom. Despite having decent savings and enough monthly income to afford rent, I was compelled to stay at home.Why?


Using html2canvas

Photo by Carlos Muza on Unsplash

I was recently tasked with implementing logic that captures react components with their data visualizations on the web page and exports it as an image. Other data visualization libraries (such as Highcharts) allow for graphs to be exported as an image. However, we needed one to export multiple graphs and tables on a page at once.

This is where html2canvas comes in handy.

This package generates a “screenshot” by reading DOM elements, their styles, and then generating a canvas image from that data. It operates purely on the client side so you don’t have to worry about any requests/server support.


Another example for how to stop overusing array functions

Photo by Farzad Nazifi on Unsplash

In a previous post, I provided a potential use case for object indexing to avoid overusing array functions in a react application. While it does illustrate the applicability of object indexing, Vachev T pointed out that it might not have been the best example as there was a more efficient alternative.

The idea is good, I just dont understand why so we have to remap the object in the useEffect and create new indexed object, why dont we simply use the original array and when mapping the list of items in the render function, we just use the index argument…


An 8-step guide to making it work

Lantern in the night
Lantern in the night
Photo by Zach Lucero on Unsplash.

create-react-app is a convenient way for React developers to initialize a new React application without having to worry about boilerplate templates, configuration, etc. It can save time and effort so you can get right into developing your app!

Theoretically, using create-react-app is as easy as this:

npx create-react-app my-app
cd my-app
npm start

However, you might run into an issue preventing create-react-app from executing — even if you’ve used it on your machine before.

In my case, I got the following message:

You are running create-react-app 4.0.0, which is behind the latest release (4.0.1). …

Matthew Croak

Code 📲💻 Wellness 🧘🏻‍♂️😌

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store