mobx provider

mobx-react中Provider和inject通过context将store注入并使得任何层级的子组件可以访问到store

 

创建store后在根组件通过Provider组件注入

 

// App.jsx
import React from "react";
import { render } from "react-dom";
import Parent from "./Parent";
import { Provider } from "mobx-react";
import stores from "../stores/index";

const App = (props) => {
  React.useEffect(() => {
    stores.UserStore.fetchUserInfo();
  });

  return <Parent />;
};

render(
  <Provider {...stores}>
    <App />
  </Provider>,
  document.getElementById("app")
);

 

 

详细:https://segmentfault.com/a/1190000022592588

posted @ 2021-08-24 11:20  Redchar  阅读(448)  评论(0编辑  收藏  举报