{"id":5490,"date":"2022-01-14T04:25:15","date_gmt":"2022-01-14T04:25:15","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5490"},"modified":"2023-02-07T11:05:04","modified_gmt":"2023-02-07T11:05:04","slug":"observer-pattern-in-react-reactjs","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/","title":{"rendered":"Observer Pattern in React \/ ReactJS"},"content":{"rendered":"<p>The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers who are apprised in the event of changes to the subject. Let us understand this with the help of the below diagram.<br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/01\/flow-chart1.png\" alt=\"flow-chart\" \/><br \/>\nThe observer pattern is used by React in rendering UI and plays an important role in Redux in terms of managing the application state.<\/p>\n<h2>React consists of components, states, and props:<\/h2>\n<ul>\n<li><strong>Components <\/strong>can be used as a modular representation of the UI.<\/li>\n<li><strong>States <\/strong>are just information or plain javascript objects that live inside components.<\/li>\n<li><strong>Props:<\/strong> When information is passed to another component, it is called a &#8220;prop.&#8221;<\/li>\n<\/ul>\n<p>In <a href=\"https:\/\/studysection.com\/blog\/what-are-react-js-components\/\">React<\/a>: Whenever a state within a component is changed, the component is re-rendered. When the state of the parent component is passed to its child component as a prop, the child component will be re-rendered when the prop is changed. As this is a recursive process, it is shown in the below diagram:<br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/01\/flow-chart1-1.png\" alt=\"flow-chart1\" \/><\/p>\n<p>If the state of the parent component is passed to the grandchild component, then the change will cause all dependent components holding that state to be re-rendered.<br \/>\nIn this diagram, observers are the components that are holding the states, and the parent is the subject that is notifying the observers.<\/p>\n<p><strong>In redux:<\/strong> In redux, states are stored in a place called a store. Below is an example of this.<br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/01\/flow-chart2.png\" alt=\"flow-chart2\" \/><br \/>\nThe store contains the modules for the state. The states are stored in a particular module. For example, all metadata information is stored in the metadata module, and all user-related information is stored in the user info module, etc.<br \/>\nThe observer pattern has nothing to do with React or Redux. It is not specific to any language or framework. It is a general pattern used in various contexts. This pattern is an example of one-to-many relationships by reducing the coupling between the objects.<\/p>\n<p><strong>Example of the observer pattern:<\/strong> Let\u2019s create an input element and an empty div.<\/p>\n<h3>HTML code:-<\/h3>\n<p><code>&lt;input type=\"input\" class=\"input\" \/&gt;<br \/>\n&lt;div class=\"d1\"&gt;&lt;\/div&gt;<\/code><\/p>\n<h3>Js code:-<\/h3>\n<p><code>class Observable {<br \/>\nconstructor() {<br \/>\nthis.observer = [];<br \/>\n}<br \/>\nsubscribe(item) {<br \/>\nthis.observer.push(item);<br \/>\n}<br \/>\nunsubscribe(item) {<br \/>\nif(!this.observer) return 'empty';<br \/>\nelse {<br \/>\nthis.observer.filter(subscribe =&gt; subscribe !== item);<br \/>\n}<br \/>\n}<br \/>\nnotify(data) {<br \/>\nthis.observer.forEach(item =&gt; item(data));<br \/>\n}<br \/>\n}<br \/>\nvar d1 = document.querySelector('.d1');<br \/>\nvar input = document.querySelector('.input');<br \/>\nconst update1 = text =&gt; d1.textContent = text;<br \/>\nvar observarble = new Observable();<br \/>\nobservarble.subscribe(update1);<br \/>\ninput.addEventListener('keyup', event =&gt; observarble.notify(event.target.value));<\/code><\/p>\n<p><em>This is all about the observer pattern in react js. <\/em><\/p>\n<p><small><em>People having good knowledge of Financial accounting can get an <a href=\"https:\/\/www.studysection.com\/financial-accountant-advanced-diploma\">Accounting Certification Exams<\/a> from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.<\/em><\/small><\/p>\n<p><script>\njQuery(\"#top-view-post\").text('Attempt React Test Now');\njQuery(\"#top-view-post\").attr(\"href\", \"https:\/\/www.studysection.com\/reactjs-foundation\");\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a<\/p>\n","protected":false},"author":1,"featured_media":5491,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[347,641],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Observer Pattern in React \/ ReactJS - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Observer Pattern in React \/ ReactJS - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-14T04:25:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-07T11:05:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/01\/React.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Observer Pattern in React \/ ReactJS\",\"datePublished\":\"2022-01-14T04:25:15+00:00\",\"dateModified\":\"2023-02-07T11:05:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\"},\"wordCount\":411,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"React\",\"Reactjs\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\",\"url\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\",\"name\":\"Observer Pattern in React \/ ReactJS - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-01-14T04:25:15+00:00\",\"dateModified\":\"2023-02-07T11:05:04+00:00\",\"description\":\"The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Observer Pattern in React \/ ReactJS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Observer Pattern in React \/ ReactJS - StudySection Blog","description":"The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/","og_locale":"en_US","og_type":"article","og_title":"Observer Pattern in React \/ ReactJS - StudySection Blog","og_description":"The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers","og_url":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-01-14T04:25:15+00:00","article_modified_time":"2023-02-07T11:05:04+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/01\/React.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Observer Pattern in React \/ ReactJS","datePublished":"2022-01-14T04:25:15+00:00","dateModified":"2023-02-07T11:05:04+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/"},"wordCount":411,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["React","Reactjs"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/","url":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/","name":"Observer Pattern in React \/ ReactJS - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-01-14T04:25:15+00:00","dateModified":"2023-02-07T11:05:04+00:00","description":"The observer pattern is one of the most common patterns in software design. In this pattern, a subject contains a list of observers","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/observer-pattern-in-react-reactjs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Observer Pattern in React \/ ReactJS"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":10755,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5490"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5490"}],"version-history":[{"count":5,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5490\/revisions"}],"predecessor-version":[{"id":6448,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5490\/revisions\/6448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5491"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}