Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Remote Job

    Top 10 issues when hiring freelancers

    Interview

    How to Hire a Freelance Web Developer: Things to Look For and Avoid

    Django

    Integrating GraphQL into Django

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / React Lessons / React Lesson 10: Normalize Data with Immutable.js
    JavaScript

    React Lesson 10: Normalize Data with Immutable.js

    Mohammad Shad MirzaBy Mohammad Shad MirzaMarch 9, 2020No Comments5 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    React Lesson 10: Normalize Data with Immutable.js
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    React Lessons. Lesson 10. Normalize Data with Immutable.js
    React Lessons. Lesson 10. Normalize Data with Immutable.js

    In this lesson, we are going to explore how we work with data. As you can see in fixtures.js, the data structure we have used so far to display articles is not normalized. Every article contains information about it, it resembles a tree-like structure.

    [
      {
      "id": "some_id_here",
      "date": "2016-06-09T15:03:23.000Z",
      "title": "random_title",
      "text": "Text...",
      "comments": [
          {
            "id": 0,
            "user": "User name",
            "text": "comment added ..."
          },
          ...
        ]
      },
      {...similar object},
      ...
    ]

    This kind of structure is readable, but it will become too overwhelming once you start changing the data. If you’ve got a pretty complicated structure where all the dependencies connect – for example, the article has an author, the comment has an author, and the author has his own page.

    If you store it in a current state, whenever you want to change the author’s name, you will have to look through all the places where it can theoretically be used and change the data.

    That’s why, before storing the data in stores they get normalized. Normalizing data simply means to store comments, articles, authors, etc. separately and store the connections separately as id.

    For example, if you’ve got a lot of comments, you can initially store an array with the id of these comments.

    Normalize articles

    Let us first introduce a normalized data structure in rendering articles, then we will do the same with comments. We will also change the way we store the data. Right now, the data are stored in the form of a usual array, which is not very convenient. If we want to work with the data, we will need to appeal a certain element, so it will be much more convenient to store the data as objects, where ids of articles are the keys and the article itself is a value.

    Step 1: Install Immutable.js

    Let us continue our lesson and use the library immutable.js by running this command:

    yarn add immutable

    or

    npm install immutable

    Immutable.js has various data structures. A few of the most popular are:

    • List is an array analog but immutable.
    • Map is an immutable object analog.
    • OrderedMap is a variation of Map, where you save the sequence of the added elements.
    • Record enables you to describe the structure your element has.
    Read More:  Conditional Rendering Methods in React

    Let us use Record and OrderedMap in our project.

    Step 2: Normalize Articles with Record and OrderedMap

    A record is similar to a JS object but enforces a specific set of allowed string keys and has default values. Go to reducers/articles.js and define the structure like this:

    import { DELETE_ARTICLE } from "../types";
    //import normalised articles
    import { normalizedArticles } from "../fixtures";
    //immutable data structure
    import { OrderedMap, Record } from "immutable";
    
    //define article record
    const Article = Record({
      id: "",
      date: "",
      title: "",
      text: "",
      comments: []
    });

    Records always have a value for the keys they define. Removing a key from the record simply resets it to the default value for that key. By doing this, we get a guaranteed structure of your data (articles), and it will always be specially arranged.

    OrderedMap is a type of Map, which has the additional guarantee that the iteration order of entries will be the order in which they were set(). Let us create OrderedMap using this Article record.

    import { DELETE_ARTICLE } from "../types";
    import { normalizedArticles } from "../fixtures";
    import { OrderedMap, Record } from "immutable";
    
    const Article = Record({
      id: "",
      date: "",
      title: "",
      text: "",
      comments: []
    });
    
    //create OrderedMap of articles
    const defaultArticles = normalizedArticles.reduce((acc, el) => {
      return acc.set(el.id, new Article(el));
    }, new OrderedMap({}));
    
    // use it as an initial list of articles to render
    const INITIAL_STATE = {
      articles: defaultArticles
    };

    Using the reduce method, we move from the first to the last element of the array by collecting them into another structure – in our case, into immutable object acc. Also, there is el – an element we’ve been using (the first and the second articles, etc). And the initial value of acc we start with is a new OrderedMap({})).

    Read More:  Daily Schedule Post

    Step 3: Change usage in a component

    Next, we will need to change our API a little bit, so let us go to containers/Filters.js and do the following changes:

    const mapStateToProps = state => {
      return {
        articles: state.article.articles.valueSeq(),
        filters: state.filters
      };
    };

    Earlier, we thought we would work with the array, and now we receive an object. That’s why we transform it inside the connect. To turn OrderedMap structure into Listarray we call the method valueSeq() – in this case, we’ll get just an article array.

    We can also have some problems within our Select component, as we’ve expected to see an array and we get a custom structure. To avoid any errors, you need to go to options attribute and modify them into toJS():

    <Select
      options={options.toJS()}
      isMulti={true}
      value={selectedArticles}
      onChange={this.handleSelectChange}
    />

    We will also need to correct containers/Articles.js and add valueSeq():

    const mapStateToProps = state => {
      return {
        articles: state.article.articles.valueSeq()
      };
    };

    Why do we call valueSeq()?

    Because articles are an object (key: value), and we want to get values out of them and move them into the array. Following the same steps, you’ve got keySeq to get the keys. You do not need to modify anything else as React perfectly understands Immutable.js and its structures. That’s why you do not need to transfer them into the native JS structures.

    Our articles are perfectly normalized and in the next lesson, we will extract out comments and normalize that too.

    You can visit the codesandbox below to see this lesson in action.


    Edit React_lesson_lesson10

    The lesson code can be found in our repository.

    react react lesson
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Mohammad Shad Mirza
    • X (Twitter)
    • LinkedIn

    JavaScript lover working on React Native and committed to simplifying code for beginners. Apart from coding and blogging, I like spending my time sketching or writing with a cup of tea.

    Related Posts

    Streamlining Resource Allocation for Enhanced Project Success

    December 18, 2024

    Conducting Effective Post-Project Evaluations: A Guide

    December 16, 2024

    Strategies for Keeping Projects on Track and Meeting Deadlines

    December 10, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Tips December 24, 2019

    5 Effective DevOps Practices You Must Follow

    Implementing or using DevOps unsuccessfully can waste a lot of time, cost a lot of money and potentially irritate a lot of workers. With that in mind, this article is going to look at five different DevOps practices that your company must follow.

    Enhancing Software Development: The Crucial Role of Version Control

    December 9, 2024

    How to Stay Motivated While Working Remotely/Freelancing?

    February 8, 2020

    True Freelancer’s story

    November 15, 2016

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Maximizing Efficiency: Utilizing Project Dashboards for Progress Tracking

    JavaScript November 28, 2024

    How to Write a Winning Software Development Project Pitch

    Startups September 23, 2019

    Top 6 Things every U.S. Company Should Do Before Hiring a Foreign Independent Contractor

    Job October 4, 2018

    Navigating Tomorrow: Innovations Shaping the Future of SaaS

    SaaS & Tech August 27, 2025

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    GraphQL

    How to create a Github Jobs app using React, Apollo, and GraphQL – Part #2

    JavaScript

    Conditional Rendering Methods in React

    Beginners

    Java Lambda Expressions

    Most Popular

    Understanding Data-binding in React and Angular

    React

    Create simple POS with React, Node and MongoDB #5: Setup ReCaptcha and define CORS

    JavaScript

    Tough Interview

    Comics
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.