Functional components in React are missing the concept of a Constructor

But what is a Constructor?

In class-based object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables. ~ wiki link

So, in simple words with a Constructor we initialize the state of our component, and someone could say that “Function components don’t need a constructor. You can initialise the state in the useState call.”

The problem here is that Function components are… functions. They run whenever you call them. Just like that. This means that we don’t have life-cycles, which also means that we don’t have a place in which we can actually initialize our component with a code that runs only once before the component is rendered for the first time. …

Image for post
Image for post
Photo by Pablo Heimplatz on Unsplash

This is the second part of the series of use of “MongoDB Realm + GraphQL + Apollo Client (on React)”. You can find the first post of the series with more details here.

This post will describe how we can update or delete one specific document in our Database.

Exactly, as in the previous post, the steps for both scenarios are the following:

  1. We figure out our query in MongoDB Explorer.
  2. We copy the query in our operations.graphql file, and we execute the $ npm run generate command.
  3. We move the logic to our components as we did on our creation scenario. …

Image for post
Image for post
Photo by Aperture Vintage on Unsplash

Recently I started a personal side project in which I needed to create a DB and consume their data.

I like to experiment on my side projects, so I decided to go with some familiar and some new technologies.

The full stack will be:

  • React.JS & Apollo Client
  • GraphQL []
  • MongoDB (using server-less Mongo Atlas + MongoDB Realm services) []

I am familiar w/ React, and Mongo and the new parts are GraphQL and the whole “server-less” Mongo Atlas thing.

In this series of posts, I’ll try to gather those things that were not trivial or caused me some confusion in the process. …


Theodoros Mathioudakis

Passionately curious. Software engineer. Track and Field. Greece.

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