{"id":512,"date":"2022-11-05T11:44:41","date_gmt":"2022-11-05T03:44:41","guid":{"rendered":"http:\/\/gjweb.top\/?p=512"},"modified":"2022-11-05T11:44:42","modified_gmt":"2022-11-05T03:44:42","slug":"mobx%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=512","title":{"rendered":"mobx\u57fa\u672c\u7528\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u5b89\u88c5\">\u5b89\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add mobx -S<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u57fa\u672c\u4f7f\u7528\">\u57fa\u672c\u4f7f\u7528<\/h2>\n\n\n\n<p>mobx \u4e3b\u8981\u7528\u5230\u4e24\u4e2a\u6a21\u5757 observable (\u89c2\u5bdf\u8005) \u3001autorun\uff08\u8ba2\u9605\u8005\uff09<\/p>\n\n\n\n<p>\u521b\u5efa mobx \/ store.js \u7528\u6765\u5b58\u653e\u9700\u8981\u89c2\u5bdf\u7684\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {observable} from \"mobx\"<br>const sotre = observable({<br> &nbsp; &nbsp;name:'hello word'<br>})<br>export default store<\/code><\/pre>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {useEffect,useState} from \"react\"<br>import store from \"mobx\/store.js\"<br>import {autorun} from \"mobx\"<br>\u200b<br>const ChildA = ()=&gt;{<br> &nbsp; &nbsp;const &#91;state,setState] = useState({name:\"\"})<br> &nbsp; &nbsp;useEffect(()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;\/** \u9875\u9762\u521d\u59cb\u5316\u4f1a\u52a0\u8f7d store \u4e2d\u7684\u6570\u636e\uff0c\u7ed1\u5b9a\u5230state\u4e2d\u6765\u66f4\u65b0\u89c6\u56fe*\/<br> &nbsp; &nbsp; &nbsp; &nbsp;autorun(()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setState((data)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return {...data,data.name:store.name}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  })<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  },&#91;])<br> &nbsp; &nbsp;\/** \u5728 mobx \u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u76f4\u63a5\u8981\u901a\u8fc7 store \u53bb\u4fee\u6539\u89c6\u56fe*\/<br> &nbsp; &nbsp;const handelClick = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;store.name = \"hello ReactMobx\"<br> &nbsp;  }<br> &nbsp; &nbsp;return \uff08<br> &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp;      &lt;h2&gt;this is ChildA {state.name}&lt;\/h2&gt;<br> &nbsp;          &lt;button onClick={handelClick}&gt;click&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp;\uff09<br>}<br>\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4e25\u683c\u6a21\u5f0f\u4f7f\u7528\u65b9\u6cd5\">\u4e25\u683c\u6a21\u5f0f\u4f7f\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<p><strong>\u6ce8\u610f mobx \u7248\u672c5 \u4e0a\u8fd0\u884c,\u65b0\u53d1\u884c\u7684\u7248\u672c6.0 \u7528\u6cd5\u6709\u5927\u7684\u53d8\u52a8<\/strong><\/p>\n\n\n\n<p>\u5728 mobx \/ store.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {observable,configure, action} from 'mobx'<br>configure({<br> &nbsp; &nbsp;enforceActions:'always'<br>})<br>\u200b<br>class Store {<br> &nbsp; &nbsp;@observable &nbsp;name = \"xxx\"<br> &nbsp; &nbsp;@action setName(){<br> &nbsp; &nbsp; &nbsp;this.name = \"hello world\"<br> &nbsp;  }<br>}<br>const store = new Store()<br>export default store<\/code><\/pre>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useEffect} from \"react\"<br>import store from \".\/mobx\/store2\"<br>import {autorun} from \"mobx\"<br>const ChildB = ()=&gt;{<br> &nbsp;useEffect(()=&gt;{<br> &nbsp; &nbsp;autorun(()=&gt;{<br> &nbsp; &nbsp; &nbsp;console.log(store.name)<br> &nbsp;  })<br>  },&#91;])<br> &nbsp;const handelClick = ()=&gt;{<br> &nbsp; &nbsp;store.setName()<br>  }<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;h2&gt;this is ChildB&lt;\/h2&gt;<br> &nbsp; &nbsp; &nbsp;&lt;button onClick={()=&gt;handelClick()}&gt;click&lt;\/button&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<br>export default ChildB<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5 \u57fa\u672c\u4f7f\u7528 mobx \u4e3b\u8981\u7528\u5230\u4e24\u4e2a\u6a21\u5757 observable (\u89c2\u5bdf\u8005) \u3001autorun\uff08\u8ba2\u9605\u8005\uff09 \u521b\u5efa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-mobx"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=512"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":513,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/512\/revisions\/513"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}