There are a few questions that I see pop up on the various blog posts I've written on React Native Meteor. I want to put together an FAQ to, hopefully, help you out and point you in the write direction. This document will be updated over time so please send me anything you feel should be included.
Make sure to point your React Native app to the correct url for your Meteor app
1. 确保你的React Native应用指向了正确的Meteor应用URL
Meteor automatically knows what your server URL is because it handles the entire stack. Since React Native is purely a client and doesn't have those built-in server bindings you need to inform it of which server you want to connect to, and it change it based on the environment. This will likely be localhost:3000 in development and something like wss://my-fake-app.meteor.com in production. Make sure you're connecting to the correct one before publishing and when debugging.
When I try to subscribe
I get an error of INVALID_STATE_ERR
, what does this mean?
2. 当我尝试订阅数据,我得到一个错误INVALID_STATE_ERR,这意味着什么?
This error means you're trying to interact with the DDP/Meteor server before you've actually connected to the server. Make sure that you point to the correct URL of your Meteor server and that you're connection has succeeded before making calls/subscriptions.
How can I identify a user?
3. 我该如何用户验证?
If you've authenticated your user via DDP then you'll have access to*this.userId *in publish functions and Meteor.userId() everywhere else. Here's a few articles on authentication with React Native and Meteor
Can I do Hot Code Push?
4. 我能代码热重载吗?
Yes! There are third party tools such as CodePush and AppHub out there to handle this. There are limitations but they're the same limitations you would face with Meteor and Cordova.
If you want to get CodePush integrated check out this article.
You only have to connect to the DDP (Meteor) server once
5. 你只需要连接一次Meteor的DDP服务器
This one may seem obvious to some of you but for those just getting started know that you only have to connect to the Meteor server once, likely when your app first mounts. Subsequent calls and subscribes will use the existing DDP connection - you don't need to specify a new one prior.
Which DDP Package should I use? Why?
6. 我该使用哪个DDP扩展?为什么?
This is a tricky one because there are a few options out there currently. Here are a few:
- ddp.js: This one is quickly becoming my go to for raw interaction with the ddp protocol. It’s actually the one that backs react-native-meteor.
- react-native-meteor: This is the one I’m really excited about and have been putting some effort behind. It’s a “magic” included DDP client — allowing you to use an extremely similar API to what you may be used to with vanilla Meteor. It’s still a work in progress but should definitely stay on your radar.
- spencercarli/node-ddp-client: This is a stop-gap solution to some of the issues with the *node-ddp-client *that I mention below. It fixes a few bugs, corrects documentation, BUT doesn't use minimongo-cache
- node-ddp-client: This is the one I’ve used for quite a while. The latest version uses minimongo-cache. Downsides include a few bugs, incorrect documentation, and it’s no longer supported.
What about Minimongo?
7. 那么Minimongo呢?
There are some efforts to replicate minimongo without the browser dependency but I, to be honest, haven't followed it too much as I've preferred to not use it in React Native. The leading contender for a replacement is minimongo-cache. It's also been integrated into react-native-meteor so my opinions on minimongo in React Native may change soon!
Be more conscious of your API and data schema up front - it's harder to update things even with CodePush
8. 加倍关注你的API和数据模式 - 即使用CodePush也相当难以更新
When you're developing with Meteor, or the web in general, you've got quite a bit of flexibility. You can publish new features and bug fixes whenever you want and know that the user will have the latest code in their hand the next time they visit your site. The same isn't true for mobile app development - this goes for Objective C, Swift, Java, React Native, etc. You've got to go through some sort of process to update your code... and then you need your user to opt in to updating.
This has gotten easier with the likes of CodePush and AppHub but it's still something to be aware of when developing for React Native. Make sure that you put some time up front designing your data structure and API so that it doesn't bite you later.
What about OAuth?
9. 那么OAuth相关的呢?
This has been the most requested item... and I've found it to be tough. The existing "magic" OAuth that comes with Meteor doesn't play too well with an external client's OAuth process due to its tight coupling with the browser. I have dived into it a bit and put together a guide for the Google OAuth process here:
React Native Meteor Google OAuth
Do you want me to write a definitive guide on React Native Meteor OAuth with the various OAuth providers? Ping me on Twitter.
Boilerplates
10. 模板
Boilerplates are a great way to get that instant gratification. You may grow out of them later but when you're learning they're extremely valuable and get you a quick win. Here are a few
- react-native-meteor: This is one I wrote and thus am partial to. It's very bare bones - handling only the DDP connection, basic authentication, and some file structure.
- react-native-meteor-starter: A bit more in depth than the other but shows you more functionality.
I'm planning to write a book on React Native and Meteor - taking you from design mockups to publishing an iOS app**AND **an Android app to their respective store’s. If you're interested in early access and being part of the feedback process sign up for my email list here.
If you have any questions just respond to one of my newsletter emails and I’ll answer it the best I can. You can also ask me on Twitter.
参考
原文地址:https://medium.com/@spencer_carli/react-native-meteor-faq-919af1db7239#.8sj6u421p