Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Development

    Enhancing Development Quality Through Effective Code Reviews

    Beginners

    Think Like a Pythonista — Building a Book Sharing App

    Python

    Powerful, Opinionated And Idiosyncratic Scripting Language Or What We Know About Python

    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
    Thursday, September 11
    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 6: CSS and Animation
    JavaScript

    React Lesson 6: CSS and Animation

    Mohammad Shad MirzaBy Mohammad Shad MirzaJanuary 4, 2020Updated:September 23, 2020No Comments5 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    React Lesson 6: CSS and Animation
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    React Lesson 6: CSS and Animation
    React Lesson 6: CSS and Animation

    Today we are going to add some styling and animation in our React app. We will also talk about the performance aspect of our app and how we can control re-rendering with the shouldComponentUpdate lifecycle method. Let’s start.

    Adding CSS

    We are going to add CSS to Article.js file but before that let’s create a separate directory that will contain both Article.js and it’s style.css.

    1. Create an Article directory and move Article.js inside it.
    2. Rename Article.js to index.js.
    3. Create a stylesheet style.css inside Article directory
    4. Update imports
      import CommentList from "../CommentList";
      import "./style.css";

    Now we will add a className property to the div we want to style

    return (
          <div className="article">
            <h1 onClick={openArticle}>{title}</h1>
            ...

    Let’s add some CSS in the stylesheet for the article className we just added.

    /* article style */
    .article {
      background-color: #d3f4ff;
      border-radius: 5px;
      padding-left: 10px;
      color: #222;
    }

    We can also add sometimes styling to the links and make it more like a button

    /* link style */
    a:link,
    a:visited {
      background-color: #f44336;
      color: white;
      padding: 14px 25px;
      margin: 14px 0px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
    }
    
    a:hover,
    a:active {
      background-color: red;
    }

    Our app is starting to look better now:

    Article list
    Article list

    Let’s understand the animation now:

    CSS Animation

    There are some addons/libraries which makes our job easier. One such library is the react-transition-group. It manages our component state over time and controls the animation after we define how we want it to be.

    Install the module

    You can use npm or yarn upon your choice.

    yarn add react-transition-group

    Import the module in Article.js and wrap the component which we want to animate

    The library provides CSSTransition which accepts some props and takes care of the animation.

    //import
    import { CSSTransition } from "react-transition-group";
    
    //inside render
    return (
      <div className="article">
        <h1 onClick={openArticle}>{title}</h1>
        <CSSTransition
          in={isOpen}
          timeout={500}
          classNames="article"
          unmountOnExit
        >
          <section>
            {text} <CommentList comments={comments} />
          </section>
        </CSSTransition>
      </div>
    );
    • in prop of CSSTransition expects a boolean value that marks the enter and exit of animation.
    • We provide a classNames prop as CSSTransition applies a pair of class names during the appear, enter, and exit states of the transition.
    • Child component receives a set of classes upon the value of in prop. Let’s add these class and animation inside style.css
    /* animation style */
    .article-enter {
      opacity: 0;
      transform: scale(0.8);
    }
    .article-enter-active {
      opacity: 1;
      transform: translateX(0);
      transition: opacity 300ms, transform 300ms;
    }
    .article-exit {
      opacity: 1;
    }
    .article-exit-active {
      opacity: 0;
      transform: scale(0.8);
      transition: opacity 300ms, transform 300ms;
    }

    These classes watch for the child element when they appear or vanish i.e. enter or exit. The CSS properties defined inside these classes will animate the child element accordingly.

    Visit this link to learn more about the properties available.

    And we are done with our simple CSS animation. Now let’s move on to enhance the performance of our app.

    Read More:  React Lesson 8: Deep Dive into React Redux

    Performance

    Let us recall how React works. It watches for state changes and updates Virtual DOM. In our case whenever we open or close any article, we are changing the app state triggering a re-render. This can lead to performance issues. Luckily, React gives us a method that can specify when we want to update our app.

    shouldComponentUpdate

    shouldComponentUpdate(nextProps, nextState)

    The default behavior of any React component is to re-render whenever the state changes. We can use shouldComponentUpdate() to let React know if a component’s output is not affected by the current change in state or props.
    Render triggers the value we return from this method (default is true). shouldComponentUpdate() is invoked before rendering when new props or states are being received. This method is not called for the initial render or when forceUpdate() is used.

    Let’s add this method in our CommentList component.

    shouldComponentUpdate(nextProps, nextState) {
      return nextProps.isOpen != this.props.isOpen;
    }

    When this method is invoked, React will know whether there is any change or not and it will re-render accordingly.
    You might be wondering if we do the same with comments:

    return nextProps.isOpen != this.props.isOpen || (nextProps.comments != this.props.comments);

    This will not work as we are receiving the same array so nextProps.comments and this.props.comments are always the same. That’s why React favors Immutable data. It simply means the data never changes or cannot be modified.

    Immutable data

    Immutable data cannot be changed once created, leading to much simpler application development. React can watch for state changes much effectively eliminating performance issues.

    Let us look at Immutable.js for instance. Using this type of data is pretty handy, looking from the very React programming paradigm point of view. React is frequently associated with functional programming. So, what ideas does functional programming use? Let us list them:

    1. Functions should be objects of the first order, i.e. you can use their variables, return them as another function’s results, transmit a function as arguments.
    2. Pure functions are those working only with internal functions. Stable functions can be used anywhere and are liked by many.
    3. Immutable data never change. These are data that were initially created and get no change. It means, to change data in a new object, a new object with changes will be created, but an old one will be accessible, too.
    4. In our case, you will always be able to write this kind of shouldComponentUpdate, where you will have an opportunity to compare old and new comments, and it will speed up your work on developing the app.
    Read More:  Advanced Node.Js: A Hands on Guide to Event Loop, Child Process and Worker Threads in Node.Js

    In your home task, you will need to read documents on Immutable.js. Next time we will talk about Redux.
    We will also need to integrate a third-party component day-picker. It will be added to our Select and present a selected range of dates by the “ArticleList for” and date. It is a calendar, where you can select a needed date and use it for filtration.

    You can check out the live playground of today’s lesson in the codesandbox below.

    Edit React_lesson_lesson6

    The lesson code together with previous lesson materials can be found here.

    react
    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
    Comics November 15, 2016

    True Freelancer’s story

    We are looking forward to meeting you on our website blog.soshace.com

    Hire a Software Engineer: Quirks of a Profession

    April 4, 2019

    Уроки React. Урок 7

    September 23, 2016

    Crafting a High-Performing Team: A Startup’s Essential Guide

    December 16, 2024

    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

    Crafting a High-Performing Team: A Startup’s Essential Guide

    Entrepreneurship December 16, 2024

    Effective Strategies for Generating B2B Leads via Podcasting

    B2B Leads December 6, 2024

    Startup Spotlight: Five Companies That Revolutionize Healthcare & Wellness

    Startups March 12, 2019

    Key Strategies for Successful Influencer Partnership Negotiations

    Influencer & Community 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
    JavaScript

    Build Real-World React Native App #9 : Implementing Remove Ads Feature

    JavaScript

    Build Real-World React Native App #3: Home Screen With React Native Paper

    JavaScript

    React Hooks + RxJS or How React Is Meant to Be

    Most Popular

    The Critical Role of Code Reviews in Software Development

    Programming

    Overview of FREE Python Programming Courses for Beginners

    Beginners

    CSS Flexbox

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

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