DEV Community

Audrea Cook
Audrea Cook

Posted on • Originally published at codewitch.dev on

3 2

A Tale of Two Classes

Yesterday, I was challenged by a potential employer (fingers crossed) to share a few JS classes I’m proud of. I had a lot of fun writing this up, and I figured I would share it with y’all, as well.

Below, you will find code samples from my React app, OurCal, which is deployed on Heroku, here. I chose this project because it was originally written in vanilla JS before I refactored it, and I thought it would be interesting to show how the classes evolved.

The vanilla JS Block class creates the 25x25 pixel grid, or block. The constructor accepts the block’s name and an array of pixels. To create the individual Pixel objects, the constructor calls the createPixels method, which loops through the pixels array and calls on the Pixel class for each element. This method returns the newly-created Pixel objects as an array to be sent to the back end.

The vanilla JS Pixel class is a simple constructor. It accepts four arguments, the x and y coordinates, the background color, and the color_variable representing the pen used to select the pixel.

After converting OurCal to a React app, the classes get a bit more complex (and fun).

Moira Rose saying, "No one will ever accuse you of being vanilla again."

The React CompleteBlock class displays a fully-colored block. It takes the block’s name and block data as props. The createBlock and createColumns methods work in tandem to populate a grid with individual Pixel objects. On componentDidMount(), the pixels are then filled with the appropriate background colors, using the colorPixels method.

The React Pixel class creates the individual pixels, based on the block’s location, which is passed in through props. If the location is Template (the editable block), a click handler is attached to the pixel. The handleClick method toggles the pixel’s className (and corresponding background color). Pixels in the Template location are also added to the Redux store through componentDidMount.

Bug Alert!

The current Heroku deployment contains a bug dealing with the toggled background colors.

Night Flight bug and screaming woman

I am aware of it and have addressed the problem in the code. I’m working on deploying the fix, hopefully, later today.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more