How to connect state to props with mobx.js @observer when use ES6 class?

前端 未结 4 1668
逝去的感伤
逝去的感伤 2021-01-30 14:54

Let\'s take a class like this in an app with React and React Router.

@observer class Module1 extends React.Component {

  constructor (props) {
    super(props);         


        
4条回答
  •  有刺的猬
    2021-01-30 15:13

    First, here is a simple example app that does routing using MobX, React and react-router: https://github.com/contacts-mvc/mobx-react-typescript

    In general, personally I like to explicitly pass all the relevant stores as explicit props to my components. But you can also use a package like Ryan to have your stores passed to your components using the React context mechanism, similar to Redux connect (see this app for an example).

    Once you have your store in your component, parse the routing params in ComponentWillMount and update your stores accordingly.

    That should basically be all :) But let me know if I let anything unanswered.

提交回复
热议问题