{"id":6883,"date":"2019-04-30T12:54:26","date_gmt":"2019-04-30T10:54:26","guid":{"rendered":"https:\/\/sii.pl\/blog\/?p=6883"},"modified":"2023-11-07T08:20:39","modified_gmt":"2023-11-07T07:20:39","slug":"sharepoint-framework-and-mobx-basic-ideas-and-apis","status":"publish","type":"post","link":"https:\/\/sii.pl\/blog\/en\/sharepoint-framework-and-mobx-basic-ideas-and-apis\/","title":{"rendered":"SharePoint Framework and MobX \u2013 Basic ideas and APIs"},"content":{"rendered":"\n<p>For the first article in this series, <a href=\"https:\/\/sii.pl\/blog\/en\/sharepoint-framework-and-mobx-how-to-make-app-data-management-easier\/?category=hard-development&amp;tag=mobx-en,office-365-en,office-365-competency-center-en,sharepoint-en,sharepoint-framework-en,spfx-en\" target=\"_blank\" rel=\"noopener\">go to this post.<\/a><\/p>\n\n\n\n<p>It\u2019s fairly easy to implement MobX in your SPFx solution. You have to know few basic concepts and then you\u2019re ready to rock\u2019n\u2019roll. There are four fundamental elements:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/SPFx-MobX-2-Idea-Diagram-Office-365.png\"><img decoding=\"async\" width=\"840\" height=\"312\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/SPFx-MobX-2-Idea-Diagram-Office-365.png\" alt=\"Diagram of MobX's basics\" class=\"wp-image-24328\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/SPFx-MobX-2-Idea-Diagram-Office-365.png 840w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/SPFx-MobX-2-Idea-Diagram-Office-365-300x111.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/09\/SPFx-MobX-2-Idea-Diagram-Office-365-768x285.png 768w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/a><\/figure>\n\n\n\n<p class=\"has-text-align-center\">Fig. 1 Diagram of MobX&#8217;s basics<\/p>\n\n\n\n<p>First of all, the <strong>state<\/strong>. It\u2019s the main data model of your app. The state is represented by observables \u2013 properties that can change over time. In other words \u2013 components are dependent of this information and you want them to react to changes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n@observable classProperty = value;\n<\/pre><\/div>\n\n\n<p>Next, there are <strong>actions<\/strong> \u2013 functions that update the state. They change the values of observables. That\u2019s their only purpose.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n@action classMethod (value){\nthis.classProperty = value;\n}\n<\/pre><\/div>\n\n\n<p>Finally, we have two elements that react to state changes \u2013 computed <strong>values<\/strong> and <strong>reactions<\/strong>. First ones are properties that are based on observable values. The easiest example is the arithmetical sum. Observable A + observable B = computed value C. Modifications of A or B cause changes of C\u2019s value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n@computed get classPropertyC() {\nreturn this.classPropertyA + this.classPropertyB;\n}\n<\/pre><\/div>\n\n\n<p>And last but not least, we encounter <strong>reactions<\/strong>. Their name is pretty self-explanatory. They\u2019re functions invoke by state changes. So, we could say that they \u2018react\u2019 to those changes. There are 3 types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2018autorun\u2019 \u2013 runs automatically, doesn\u2019t return new value<\/li>\n\n\n\n<li>\u2018when\u2019 \u2013 waits for given statement to be true, then runs side effect<\/li>\n\n\n\n<li>simply \u2018reaction\u2019 \u2013 waits for an update of property, then runs side effect<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nautorun(() =&gt; { sideEffect }, options)\n\nwhen(() =&gt; condition, () =&gt; { sideEffect }, options)\n\nreaction(() =&gt; data, data =&gt; { sideEffect }, options)\n<\/pre><\/div>\n\n\n<p>One more thing to know is the concept of the observer decorator. It\u2019s used for components that use observables in their \u2018render\u2019 methods. If those observables change, our components rerender.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n@observer class MyComponent extends React.Component { ... }\n<\/pre><\/div>\n\n\n<p>To sum up:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>properties marked @observable change over time and create app state,<\/li>\n\n\n\n<li>functions marked @action update the state,<\/li>\n\n\n\n<li>properties marked @computed are calculated based on observables,<\/li>\n\n\n\n<li>state changes invoke functions called &#8216;autorun&#8217;, &#8216;when&#8217; or &#8216;reaction&#8217;,<\/li>\n\n\n\n<li>classes marked @observer are dependent on observables.<\/li>\n<\/ul>\n\n\n\n<p>How to actually use all these elements in our SPFx solutions? We\u2019re going to find out in the third and final article in our MobX article series.<\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;6883&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;2&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;11&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;5\\\/5 ( votes: 2)&quot;,&quot;size&quot;:&quot;18&quot;,&quot;title&quot;:&quot;SharePoint Framework and MobX \u2013 Basic ideas and APIs&quot;,&quot;width&quot;:&quot;139.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} ( {votes}: {count})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 139.5px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 14.4px;\">\n            5\/5 ( votes: 2)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>For the first article in this series, go to this post. It\u2019s fairly easy to implement MobX in your SPFx &hellip; <a class=\"continued-btn\" href=\"https:\/\/sii.pl\/blog\/en\/sharepoint-framework-and-mobx-basic-ideas-and-apis\/\">Continued<\/a><\/p>\n","protected":false},"author":546,"featured_media":25156,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_editorskit_title_hidden":false,"_editorskit_reading_time":1,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","inline_featured_image":false,"footnotes":""},"categories":[1320],"tags":[1345,1346,1446,1444,1445],"class_list":["post-6883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hard-development","tag-sharepoint-en","tag-office-365-en","tag-mobx-en","tag-sharepoint-framework-en","tag-spfx-en"],"acf":[],"aioseo_notices":[],"republish_history":[],"featured_media_url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/10\/Porownanie-wydajnosci-mechanizmu-Qt-Signals-i-Slots-oraz-natywnej-implementacji-C.jpg","category_names":["Hard development"],"_links":{"self":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/6883"}],"collection":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/users\/546"}],"replies":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/comments?post=6883"}],"version-history":[{"count":3,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/6883\/revisions"}],"predecessor-version":[{"id":24330,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/6883\/revisions\/24330"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media\/25156"}],"wp:attachment":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media?parent=6883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/categories?post=6883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/tags?post=6883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}