mobx-devtools icon indicating copy to clipboard operation
mobx-devtools copied to clipboard

React Native compatibility?

Open southerneer opened this issue 7 years ago • 9 comments

Any plans for this?

southerneer avatar Jan 12 '18 23:01 southerneer

Yeah, definitely!

andykog avatar Jan 13 '18 09:01 andykog

I am curious how this could work. I mean with React Native you have the packager running on the computer, you can possibly get app's source from there. However, the app is running either on a connected device or in the emulator. It's not like you have a browser you can interact with.

What could be a good fit though is react-native-debugger which already has integration with Redux. I assume it could work the similar way? Can you possibly join forces together on this matter?

danielkcz avatar Feb 11 '18 11:02 danielkcz

I was able to make this work

  1. yarn add mobx-devtools mobx-devtools-mst
  2. In your index.ios.js add
import makeInspectable from 'mobx-devtools-mst';
import { connectToDevTools } from 'mobx-devtools/lib/mobxDevtoolsBackend';

// 1. Connect to devtools (what ever mobx-devtools standalone app tells you to put in)
connectToDevTools({ host: 'localhost', port: '8098' });

// This is a dummy store
const MyStore = types.model('MyStore', { foo: 'bar' }).create();

// 2a. I use react-native-navigation, so I can do
Navigation.events().registerAppLaunchedListener(() => {
  makeInspectable(MyStore);
  startApp();
});

// 2b. If you don't use RNN you need a way to know when the devtools are connected, so 
// for the time being, use a timeout or other event handler you know works.
setTimeout(() => {
  makeInspectable(MyStore);
}, 1000);

Then the rest

  1. npm install --global mobx-devtools
  2. mobx-devtools
  3. react-native run-ios

birkir avatar Jul 18 '18 16:07 birkir

Works, thank you! The only problem I have is that if I am on the Components tab in Mobx Devtools it crashes the app. screenshot 2018-12-14 at 11 13 14

elizamaria avatar Dec 14 '18 09:12 elizamaria

Hmm, they are using addEventListener on the document, you could try to just polyfill this. Let me know if it works! 👍

global.document = {
  addEventListener() {},
  removeEventListener() {}
};

birkir avatar Dec 14 '18 15:12 birkir

I don't get the same error, but there is a traverse(document) method and it crashes there.

elizamaria avatar Dec 17 '18 10:12 elizamaria

Is there any elegant way to use mobx debugging please?

SagaciousLittle avatar Apr 11 '19 03:04 SagaciousLittle

I connected MST to react-native-debugger with next steps:

  1. npm i --save mst-middlewares
  2. npm i --save-dev remotedev
  3. modify your app:
import { AppRegistry } from 'react-native'
import { Provider } from 'mobx-react';
import { types as t } from 'mobx-state-tree';
import { connectReduxDevtools } from 'mst-middlewares';
import remotedev from 'remotedev';

import MyApp from './src/components/MyApp'

const RootStore = t.model('store', {
  some: t.optional(SomeModel, {}),
  other: t.optional(OtherModel, {}),
});

const store = RootStore.create();

connectReduxDevtools(remotedev, store);

const App = () => (
  <Provider store={store}>
    <MyApp />
  </Provider>
);

AppRegistry.registerComponent(appName, () => App);

This case works only with react-native-debugger, but not chrome redux-devtools.

max-kim avatar Apr 29 '20 13:04 max-kim

One thing missing from the above is passing a redux-compatible store to <Provider store={reduxStore}>, like so:

const reduxStore = asReduxStore(store); // Where asReduxStore is imported from mst-middlewares.

tmaly1980 avatar Sep 13 '20 02:09 tmaly1980