Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Beginners

    Public Speaking for Developers Demystified: Tips & Tricks

    Events

    Two 2017 Collaboration Software Awards Now Belong To Soshace

    JavaScript

    Introduction to the Best Code Playgrounds: JSFiddle, Codepen, and CodeSandbox

    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
    Tuesday, September 9
    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 3: Exploring the React Component Lifecycle
    JavaScript

    React Lesson 3: Exploring the React Component Lifecycle

    Ilia DzhiubanskiyBy Ilia DzhiubanskiyNovember 14, 2019Updated:January 8, 2020No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    React Lesson 3: Exploring the React Component Lifecycle
    Diving deep into the component lifecycle
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    a stylized React logo
    Diving deep into the component lifecycle

    To make your code supportable, we need to specify what types of props our component can expect.

    Add the following to your comments.js file:

    Comment.propTypes = {  
        comment: PropTypes.shape({  
            text: PropTypes.string.isRequired,  
            user: PropTypes.string  
        }) 
    

    We’re expecting to receive comments from users as evidenced in Line 2 in the code snippet above. In .shape, we define what data type we’re expecting — some examples are text and user, which we’ve used just now.

    Let’s add this line to Article.js:

    First, you have to install the PropTypes package. Run npm install prop-types: or yarn install prop-types from the project folder.

    Then, change your first line to:

    import React, { Component } from 'react'
    import PropTypes from 'prop-types';
    

    Your class will look just like that:

    class Article extends Component {  
        state = {  
            isOpen: false  
        }  
        static propTypes = {  
            article: PropTypes.object.isRequired  
        }
    }  
    

    Let’s talk about decorators now. What are they? They are a function which can decorate our component (i.e. fill it with some functionality). In return, this function returns a new component. Let’s write this decorator for comments.

    import React from 'react'  
      
    export default (Component) => class DecoratedComponent extends React.Component {  
        state = {  
            isOpen: false  
        }  
      
        toggleOpen = (ev) => {  
            ev.preventDefault()  
            this.setState({  
                isOpen: !this.state.isOpen  
            })  
        }  
      
        render() {  
            return <Component {...this.props} isOpen = {this.state.isOpen} toggleOpen = {this.toggleOpen}/>  
        }  
    }
    

    In our case, we’ll finally enrich an old component with some new functionality.

    Let’s also import a decorator to CommentList:

    import toggleOpen from './decorators/toggleOpen'

    We’ll describe our class just the same way as we did before, but we’ll also provide a wrapper around it. Change the last line:

    export default toggleOpen(CommentList)

    Let’s add State and toggleOpen to the decorator by removing them from the CommentList file. For our component to be aware of those states we describe, we write about states as props in Line 16. Thus, our component will not only contain comments, but also isOpen and toggleOpen. Add a note about it to CommentList:

    const { comments, isOpen, toggleOpen } = this.props

    Remove the lines:

    const { isOpen } = this.state

    And change the OnClick line as follows:

    <a href="#" onClick = {toggleOpen}>{linkText}</a>

    This approach is far more convenient and clear. This is how your CommentList will look like:

    import React, { Component, PropTypes } from 'react'  
    import Comment from './Comment'  
    import toggleOpen from './decorators/toggleOpen'  
      
    class CommentList extends Component {  
        render() {  
            const { comments, isOpen, toggleOpen } = this.props  
            if (!comments || !comments.length) return <h3>no comments yet</h3>  
      
            const commentItems = comments.map(comment => <li key = {comment.id}><Comment comment = {comment}/></li>)  
            const body = isOpen ? <ul>{commentItems}</ul> : null  
            const linkText = isOpen ? 'close comments' : 'show comments'  
            return (  
                <div>  
                    <a href="#" onClick = {toggleOpen}>{linkText}</a>  
                    {body}  
                </div>  
            )  
        }  
    }  
      
    export default toggleOpen(CommentList) 
    

    The components that we created are rather simple. Let’s look at the component life cycle in React:

    Mounting is the first stage, before the component initialization and its emergence on the DOM tree (this block is marked with a red square on the picture). You can use the constructor to setup initial state for the component. State can be also changed in the getDerivedStateFromProps, but it’s typically reserved to rare cases like animation or data fetch based on props. You can read more about this topic here.

     

    (click to open a larger version)

    Please notice that this lifecycle was changed since earlier React versions.

    Read More:  A comprehensive insight of the Angular Structure

    Next we do rendering, build a virtual DOM and later, when we’re ready to insert it into a real DOM, we request componentDidMount. Sometimes (just like with CommentList),  our component contains a number of other components: for example, Comment in our case, which has its own Lifecycle methods. So, what would be the proper execution order? First, a parent constructor and getDerivedStateFromProps have to be operational, then render and later, the same methods will be called for the child. After that a render method is over and it starts to get built in a DOM tree. First, componentDidMount of child components finishes its work and only once all child components are ready, a parent componentDidMount will be requested.

    Next stage in the component Lifecycle is the update stage, marked with green. It will be executed in three cases: change of the state, change of the props, or call of forceUpdate. Same as in mounting phase, you can use getDerivedStateFromProps if a component state depends on props – it means, you need to control, when they get updated, and change “state” to support its current and updated state.

    That’s why you can return new state from getDerivedStateFromProps. It means that the app won’t do rendering more than once. Next render will take place with ComponentDidUpdate (where we’ve already updated). In ComponentDidUpdate, we will have access to both old and new props; while the new ones will be kept in “this,” and the old ones will stay as an argument (prevProps, nextProps). TheshouldComponentUpdate method will be studied in the upcoming lessons.

    This scheme works until the component gets fully updated, which will lead to componentWillUnmount – this means that the component is still in DOM but will soon be removed from there. It’s a common situation for clearing some sort of subscriptions.

    To understand the working principle of Lifecycle better, let us add the following coding part to class CommentList of our component CommentList:

    constructor() {  
        console.log('---', this.props)  
    }  
    componentDidMount() {  
        console.log('---', 'mounted')  
    }  
      
    static getDerivedStateFromProps(props, state) {  
        console.log('---', props.isOpen)  
    }  
      
    componentWillUnmount() {  
        console.log('---', 'unmounting')  
    

    The work result can be seen in console.

    During the Lifecycle stage the component in our DOM tree can be added with new props, and it will be constantly updating. Remember the keys from our previous lessons — we coded the basis for our Articles in those. If we don’t use them, every time our article gets removed for the list, our component will be updated – this means that it won’t get through all Lifecycle stages. But if a key changes, it will force the re-initialization of the whole component. If a key changes, React will remove the previous one and add a new one – it’s extremely convenient if you use third-party libraries like D3 in order to avoid managing all of the components manually.

    Sometimes you will need to work with real DOM. React knows this structure – you only need to request it. Use ref. for this purpose. For example, let us add ref to CommentList for an element with Link:

    <a href="#" onClick = {toggleOpen} ref={this.togglerRef}>{linkText}</a>

    Write the following coding piece in the same file:

    constructor(props) {
        super(props);
        this.togglerRef = React.createRef();
      }
    componentDidMount() {
        console.log('---', 'mounted', this.togglerRef.current)
    

    Right now we’ve got an access to a real DOM element with Link and can work with it. How can we work with JQuery components? For that purpose, let’s create a file for this JqueryComponent. Write the following:

    import React, { Component, PropTypes } from 'react'  
      
    class JqueryComponent extends Component {  
        static propTypes = {  
      
        };  
       
        constructor(props) {
            super(props);
            this.component = React.createRef();
       }
        componentDidMount() {  
            console.log('---',this.component)  
        }  
      
        render() {  
            return (  
                <div ref={this.component}/>  
            )  
        }  
    }  
      
    export default JqueryComponent 
    

    For example, this kind of construction will be OK for initializing a JQuery plugin. It isn’t the best practice, though, but sometimes it’s necessary in order to avoid too complicated solutions.

    Read More:  How to create a Github Jobs app using React, Apollo, and GraphQL - Part #2

    Let’s talk about data flows. Sometimes we cannot organize a data flow only in one direction and use a reversed data flow. By default, all data in React gets delivered downwards, but sometimes there can be a need to manage a parent component from a child one. For example, we want to have one article opened at a certain time period and others need to be automatically closed. It means that the information will be kept above, in the parent, and you need to organize a reverse data flow as our components know nothing about each other.

    First, we cannot use Functional Component Article List anymore, as we will get a Functional list. Let us start our work on ArticleList coding.

    Let’s change import coding in the first line, create class instead of a function, and add “render.” Do not forget to change “import” in the second line. Now let’s create “state” in the fifth line. To do this, we need to know what article is opened; let’s describe it in Line 6. You will also need a method that will be able to open and close your article, so let’s create it in Line 9. In Article we will give the state and function of opening the article – Line 20.

    import React, { Component }  from 'react'  
    import Article from './Article'  
      
    class ArticleList extends Component {  
        state = {  
            openArticleId: null  
        }  
      
        openArticle = id => ev => {  
            if (ev) ev.preventDefault()  
            this.setState({  
                openArticleId: id  
            })  
        }  
      
        render() {  
            const { articles } = this.props  
      
            const listItems = articles.map((article) => <li key={article.id}>  
                <Article article = {article}  
                    isOpen = {article.id == this.state.openArticleId}  
                    openArticle = {this.openArticle(article.id)}  
                />  
            </li>)  
            return (  
                <div>  
                    <h1>Article list</h1>  
                    <ul>  
                        {listItems}  
                    </ul>  
                </div>  
            )  
        }  
    }  
    

    Now you need to change the article by removing unnecessary coding and getting all needed data from props:

    import React, { Component} from 'react'  
    Import PropTypes from 'prop-types'
    import CommentList from './CommentList'  
      
    class Article extends Component {  
      
        static propTypes = {  
            article: PropTypes.object.isRequired  
        }  
    
        render() {  
            const { isOpen, openArticle, article: { title, text, comments } } = this.props  
                const body = isOpen ? <section>{ text } <CommentList comments = {comments} /></section> : null  
      
                return (  
                    <div>  
                        <h1 onClick = {openArticle}>{ title }</h1>  
                        {body}  
                    </div>  
                )  
        }  
    }  
      
    export default Article
    

    Please download the source code here.

    Your Home Assignment

    You need to create a mixin and decorator for the functionality that we’ve described in this lesson: display of only one opened article, as well as an option of closing all articles and displaying only its titles.

    a mockup web page of our blog at blog.blog.soshace.com
    See you soon!

    Previous lessons can be viewed here: https://blog.soshace.com/category/javascript/react/react-lessons/

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Ilia Dzhiubanskiy

      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
      JavaScript September 30, 2016

      If Trello became too small for you.

      Where should you go if Trello became too small for you.

      There are a number of tools for managing tasks and projects, one of them is – Trello, a great visualization’ tool. However, when your team grows and appearance of a new roles is creating many difficulties that cannot be solved within Trello – you have right solution to choose. Let me introduce Kaiten – processes’ control tool, that offers problems’ solution for growing company.

      Effective LinkedIn Outreach: Proven Strategies for Prospects

      November 26, 2024

      10 Sorting Algorithms Interview Questions: Theory and Practice for 2019

      June 7, 2019

      How to Create a Personal Blogging Website: Front-End (Angular) #2

      March 2, 2020

      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

      Yarn vs. npm in 2019: Choosing the Right Package Manager for the Job

      Beginners June 11, 2019

      Development With LAMP Stack Illustrated Address Book Project

      Beginners July 22, 2020

      Step-by-Step Guide to Building a RESTful API Effectively

      Programming December 6, 2024

      7 Statistics About Remote Work to Make Your Company Better

      Remote Job February 25, 2019

      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
      Medical Marketing

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      Business Growth

      How to Build a Predictable Lead Pipeline for Your MSP Business

      Wiki

      Автоматическое добавление в задачи ссылок на коммиты

      Most Popular

      Flask vs. Django: Let’s Choose the Right Framework for the Job

      Programming

      How to Stay Motivated While Working Remotely/Freelancing?

      Remote Job

      Adding backend to CodePen snippet using Python/Django | Question-Answering Application

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

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