Using Normalizr to organize data in stores — practical guide

Solve the issues with data organizing:  #ReactJS #JavaScript

  • Using Normalizr to organize data in stores — practical guideA React application usually needs some data from the server to be stored locally for immediate use, mainly to be shown on the page.
  • In the response for teachers we get something like this (shape of a server response): [ { students: [ { id, name, studentCourses: [ { studentId, courseId, grande, }, … ], }, … ], courses: [ { id, name, }, … ], }, … ]Here you can read about how to combine data in queries, if you also use Loopback.In this particular case this would spare us two requests, but this is not very useful if we speak about storing on front-end.
  • Secondly, we might need courses inside of a student entity instance, but when we make a query to the server, we don’t include these courses to avoid duplication.To cope with the described issues we can start with Normalizr — a utility that normalizes data with nested objects, just like in our case.
  • If you haven’t used redux-saga in your projects yet, I think this should convince you to do so.The first saga will fetch data:const urls = { teachers: ‘/teachers, students: ‘/students’, courses: ‘/courses’,};export function* fetchModel(model, ids, include) { const url = urls[model]; const where = {id: {inq: ids}}; const filter = {where, include}; const params = {filter}; return yield get({url, params});}The second will store the data:export function* queryModels(modelName, ids, include]) { const singleModelSchema = schema[modelName]; const denormalizedData = yield fetchModel(modelName, ids, include); const normalizedData = normalize(denormalizedData, [singleModelSchema]); const {entities} = normalizedData; yield a reducer will add new pieces of data to the already fetched ones:case ADD_ENTITIES: { const models = action.entities; const newState = cloneDeep(state); return mergeWith(newState, models);}Methods mergeWith and cloneDeep here are from lodash.Having done all that we can query data from server in this manner (selector):export function* fetchTeachers() { yield queryModels(‘teachers’, ids, [ { relation: ‘students’, scope: { include: [‘studentCourses’], } }, { relation: ‘courses’, } ]);}Normalizr uses a normalization schema as described here.Eventually we end up with the state that looks like this: state: { … models: { teachers: {…}, sudents: {…}, courses: {…}, studentCourses: {…}, }, … }This basically is a nice little copy of a part of our database in the store.
  • It is done in queryModels saga and we always know where the fetched data is going to be put.After that we can use it in any page of the app combing it in selectors as required.In our case, if needed, we can get an object for teacher as complicated as this (denormalized data): { students: [ { id, name, studentCourses: […], courses: […], }, … ], courses: [ { id, name, sudents: […], teachers: […], }, … ], }There is also another way.

In one of the projects, we encountered the issues with data organizing. To cope with that we can use Normalizr — a utility that normalizes data with nested objects.
Continue reading “Using Normalizr to organize data in stores — practical guide”

Maintaining a fork of create-react-app as an alternative to ejecting

Maintaining a fork of create-react-app as an alternative to ejecting:  #ReactJS

  • How everything worksFacebook’s create-react-app is a multi-package repository, powered by Lerna.Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm.
  • © Docs.So create-react-app is just a shell storing all its packages in a /packages folder.
  • After you install it, it runs a postinstall command from the main package.json, which in turn runs Lerna’s bootstrap script, which does some magic: basically moves or symlinks nested packages into your main node_modules folder allowing to require or import them as usual.The only package you need to bother about is located in /packages/react-scripts.
  • It exposes all the configuration and scripts your own project (if it was generated by create-react-app), uses to start, build, etc.
  • Have a look at scripts section of your project’s package.json.Moreover, react-scripts is the only dependency left in your package.json after create-react-app have initialised.

I started using create-react-app instead of configuring build tools (webpack, babel, etc.) by myself a while ago and it saved me a lot of time and frustration. Thanks, Facebook! But.. Sometimes you…
Continue reading “Maintaining a fork of create-react-app as an alternative to ejecting”

Why Do Developers Now Compare Vue.js to JavaScript Giants Angular and React?

  • It was 2016 when Vue.js rose to fame and gained so much traction that it’s now seen as an alternative for even well established JavaScript libraries, such as Angular and React.
  • Since Angular belongs to search engine giant Google and React comes from social media behemoth Facebook, developers still trust and use those libraries more than Vue.js.
  • This blog attempts to shed light on why there is a growing belief among developers that Vue.js is set to become the top JavaScript library, leaving behind Angular and React.
  • The reason many developers are turning to Vue is that it addresses the issues of React and Angular.
  • But, one must not forget that Angular and React are still very big names in the JavaScript world, whereas Vue has just begun to rise.

Vue.js, an MIT-licensed open source project, is a JavaScript library for building web interfaces. The library was first released in 2013, but not many developers took cognizance of it in web framework technology for the next two years. It was 2016 wh
Continue reading “Why Do Developers Now Compare Vue.js to JavaScript Giants Angular and React?”

React.js Structures JavaScript for Easy Componentization  

  • JavaScript is in the midst of transforming development teams’ expectations for desktop, server and mobile apps.
  • Enhancements to JavaScript are enabling it to break free of the browser, while technologies such as Node.js and its package manager npm facilitate creation of portable front-end and back-end apps that rival those traditionally created for environments such as Windows, Linux and mobile devices.
  • Components encapsulate the necessary HTML, JavaScript and CSS for a particular component, and apps are built by composing components into larger components that encapsulate their own data and logic.
  • React and Facebook provide a number of recommendations and best practices to help developers build their apps, but developers ultimately shoulder the burden of building the app — creating or procuring the various components, designing the data architecture and testing plan, establishing the look and feel of the app, adapting to necessary display environments, and so forth.
  • Many of the ExtReact components leverage the data package to provide consistent buffering, sorting, filtering and pagination capabilities with minimal developer effort.

JavaScript is in the midst of transforming development teams’ expectations for desktop, server and mobile apps. Enhancements to JavaScript are enabling it to break free of the browser, while technologies such as Node.js and its package manager npm facilitate creation of portable front-end and back-end apps that rival those traditionally created for environments such as Windows, Linux and mobile devices.
Continue reading “React.js Structures JavaScript for Easy Componentization  “

React Progressive Web Apps — Part 1 – Gethyl George Kurian – Medium

  • How to create your first PWA app and/or convert your existing app into PWA.In this example I created new React app with PWA in mind, but the idea should be quite similar if you have to convert your existing app into PWA.You need to understand the lifecycle of a SW, which are listed below, and you can read more about it in this link.Register the Service Worker(SW)If a browser supports SW, then the service worker will be registered.
  • Here is the code snippet from index.htmlRegister SW if browser supportsIf the above registration step was successful, then the SW file will be downloaded.
  • This would be ideal place to clean up the cache to remove any cache item which is no longer required maybe.Apart from the above lifecycle of SW, if you noticed in the earlier section, I mentioned to also go through the Fetch API.
  • Well that is because SW can intercept the fetch event, and this will help if you want to cache something from the network.Updating Cache / Making network calls — Fetch eventSW listens to fetch event, and in our example since we handle only the basic case of caching only the static files, you can see below, when there is a fetch event within the SW’s scope (Refer the registration step to see the scope),it is intercepted, and we checked if request has a response already in the cache, then we return the response from cache.
  • Adding your app to the Home Screen.Perfect!Step 1 above showed you the importance of SW and how it helps to make your app to be offline first.Step 2, tells you how you can run it from your virtual device.And now let us make it feel a bit more like an app, but letting you add this to your homepage, so that you can launch it anytime later without the need to remember the url or bookmark it.This is where you need to under about web manifest json file, which was one of the topic I mentioned would be good to know.

Progressive Web Apps(PWA) are gaining a lot of popularity these day, and with one of the updates this year (2017), the Create React App creates your project by default as PWA. If you haven’t heard of…
Continue reading “React Progressive Web Apps — Part 1 – Gethyl George Kurian – Medium”

Using Horizon/RethinkDB with React

  • Horizon, which is built on RethinkDB and by the RethinkDB team, facilitates common application development tasks like backend setup, real-time data transfer, security and scalability.
  • This tutorial will give you an introduction to Horizon and RethinkDB while also showing you how to build a simple app with Horizon and React.
  • Before hitting the code, let’s learn a few basic Horizon concepts:

    Horizon groups data into horizon collections and each collection is backed by a RethinkDB table.

  • Here’s a visual to help you make sense of the data flow in this app:

    Note that any change in the data is sent from one React client into Horizon and Horizon sends it back to all React apps so they can update their view, this flow makes sure that all clients are always watching the same without inconsistencies.

  • Our app will use two Horizon collections, one for the “we need list” and other for the “we have list”.

Horizon is an open source Backend as a Service (BaaS) that allows developers to easily build data-driven web and mobile applications. Horizon, which is built on RethinkDB and by the RethinkDB team, facilitates common application development tasks like backend setup, real-time data transfer, security and scalability. Horizon can be run locally, on a private server or in the cloud.
Continue reading “Using Horizon/RethinkDB with React”

Maintaining a fork of create-react-app as an alternative to ejecting

Maintaining a fork of create-react-app as an alternative to ejecting:  #ReactJS

  • How everything worksFacebook’s create-react-app is a multi-package repository, powered by Lerna.Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm.
  • © Docs.So create-react-app is just a shell storing all its packages in a /packages folder.
  • After you install it, it runs a postinstall command from the main package.json, which in turn runs Lerna’s bootstrap script, which does some magic: basically moves or symlinks nested packages into your main node_modules folder allowing to require or import them as usual.The only package you need to bother about is located in /packages/react-scripts.
  • It exposes all the configuration and scripts your own project (if it was generated by create-react-app), uses to start, build, etc.
  • Have a look at scripts section of your project’s package.json.Moreover, react-scripts is the only dependency left in your package.json after create-react-app have initialised.

I started using create-react-app instead of configuring build tools (webpack, babel, etc.) by myself a while ago and it saved me a lot of time and frustration. Thanks, Facebook! But.. Sometimes you…
Continue reading “Maintaining a fork of create-react-app as an alternative to ejecting”