{"id":510,"date":"2022-11-05T11:43:46","date_gmt":"2022-11-05T03:43:46","guid":{"rendered":"http:\/\/gjweb.top\/?p=510"},"modified":"2022-11-22T21:21:50","modified_gmt":"2022-11-22T13:21:50","slug":"mobx-react%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=510","title":{"rendered":"mobx-react\u4f7f\u7528"},"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@5 mobx-react@5 -S<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4f7f\u7528\">\u4f7f\u7528<\/h2>\n\n\n\n<p>\u521b\u5efa mobx \/ store.js \u7528\u4e8e\u5b58\u653e\u72b6\u6001\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {observable,action} from \"mobx\"<br>class Store {<br> &nbsp; &nbsp;@observable number = 0; \/\/ \u89c2\u5bdf\u5bf9\u8c61<br> &nbsp; &nbsp;@action add = ()=&gt;{ \/\/\u4e25\u683c\u6a21\u5f0f\u4e0b\u5b9a\u4e49\u7684\u5bf9\u5916\u66b4\u9732\u7684\u4fee\u6539\u65b9\u6cd5<br> &nbsp; &nbsp; &nbsp; &nbsp;this.number = 111<br> &nbsp;  } &nbsp; &nbsp;<br>}<br>const store = new Store()<br>export default store<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa app \u7ec4\u4ef6 (app \u7ec4\u4ef6\u8868\u793a\u7236\u7ec4\u4ef6)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>import ChildA form \".\/childA\"<br>import ChildB from \".\/childB\"<br>import store from \".\/mobx\/store.js\"<br>import {Provider} from \"mobx-react\"<br>class Index extends React.component{<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp;      &lt;Provider store={store}&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;ChildA \/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;ChildB \/&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/Provider&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>}<br>export default Index<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u7c7b\u7ec4\u4ef6\u4e2d\u7684\u4f7f\u7528\">\u7c7b\u7ec4\u4ef6\u4e2d\u7684\u4f7f\u7528<\/h3>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u8c03\u7528 ChildA.jsx \u6a21\u62df\u7c7b\u7ec4\u4ef6\u7684\u4f7f\u7528\u65b9\u5f0f<\/p>\n\n\n\n<p>\u7ecf\u8fc7\u9ad8\u9636\u51fd\u6570\u7684\u5305\u88c5\u540e, \u539f\u59cb\u7684\u7ec4\u4ef6\u7684 props \u4e2d\u5c31\u643a\u5e26\u4e86 store \u7684\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>import {inject,observer} from \"mobx-react\"<br>\u200b<br>@inject(\"store\")<br>@observer<br>class ChildA extends React.component{<br> &nbsp; &nbsp;handelClick = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.props.store.add()<br> &nbsp;  }<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;h2&gt;this is ChildA {this.props.store.number}&lt;\/h2&gt;<br>             &nbsp; &nbsp;&lt;button onClick={handelClick}&gt;click&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u51fd\u6570\u7ec4\u4ef6\u4e2d\u7684\u4f7f\u7528\">\u51fd\u6570\u7ec4\u4ef6\u4e2d\u7684\u4f7f\u7528<\/h3>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u4e2d\u8c03\u7528 ChildB \u4e2d\u4f7f\u7528\u51fd\u6570\u7ec4\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>import {observer} from \"mobx-react\"<br>import store from \".\/mobx\/store\"<br>const ChildB = ()=&gt;{<br> &nbsp; &nbsp;return (<br> &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp;      &lt;Observer&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      &lt;h2&gt;this is childB {store.name}&lt;\/h2&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  )<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/Observer&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp;  )<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u51fd\u6570\u7ec4\u4ef6\u4e2d\u7684\u9ad8\u9636\u7ec4\u4ef6\u7528\u6cd5\">\u51fd\u6570\u7ec4\u4ef6\u4e2d\u7684\u9ad8\u9636\u7ec4\u4ef6\u7528\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"\r\nimport {inject,observer} from \"mobx-react\"\r\nconst ChildC = (props)=>{\r\n    const handleClick = ()=>{\r\n       props.store.add()\r\n    }\r\n    return (\r\n    \t&lt;div>\r\n        \t&lt;p>this is ChildC {props.store.number}&lt;\/p>\r\n            &lt;button onClick={()=>handleClick()}>click&lt;\/button>\r\n        &lt;\/div>\r\n    )\r\n}\r\nexport default inject(\"store\")(observer(ChildC))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5 \u4f7f\u7528 \u521b\u5efa mobx \/ store.js \u7528\u4e8e\u5b58\u653e\u72b6\u6001\u6570\u636e \u521b\u5efa app \u7ec4\u4ef6 (app \u7ec4\u4ef6\u8868\u793a\u7236\u7ec4 [&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-510","post","type-post","status-publish","format-standard","hentry","category-mobx"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/510","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=510"}],"version-history":[{"count":2,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":518,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions\/518"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}