
将您的React本机应用程序连接到流星服务器,并利用流星特定的功能,例如帐户,反应性数据跟踪器等。与最新版本的React Native兼容。
流星指南·初学者研讨会·完整的API文档·示例项目·入门模板(EXPO) ·更多的软件包和资源
npm install --save @meteorrn/core@react-native-community/netinfo@react-native-async-storage/async-storage@>=1.8.1已安装。如果您使用的是Expo,或者无法使用@react-native-async-storage/async-storage ,请阅读以下该软件包默认情况下使用@react-native-async-storage/async-storage 。如果您使用某些React本机版本或使用Expo,这可能会导致问题。要使用自定义Asyncstorage实现,请将其作为Meteor.connect中的选项传递:
import { AsyncStorage } from 'react-native' ;
// ...
Meteor . connect ( 'wss://myapp.meteor.com/websocket' , { AsyncStorage } ) ;如果您自己使用AsyncStorage API,则重要的是您使用Meteorrn使用的相同版本,或者由于矛盾的版本而可能引起的问题。
确保您使用的是与流星(或@react-native-async-storage/async-storage如果您什么都不传递))中的同样的Asyncstorage,或者可以使用Meteorrn的软件包接口。
此示例显示了如何将Expo的Secure Store实现作为异步存储。请注意,在Android和iOS中的安全存储都具有少数兆字节的最低尺寸极限。
import * as SecureStore from 'expo-secure-store' ;
// ...
Meteor . connect ( 'wss://myapp.meteor.com/websocket' , {
AsyncStorage : {
getItem : SecureStore . getItemAsync ,
setItem : SecureStore . setItemAsync ,
removeItem : SecureStore . deleteItemAsync ,
} ,
} ) ; import Meteor , { Mongo , withTracker } from '@meteorrn/core' ;
// "mycol" should match the name of the collection on your meteor server,
// or pass null for a local collection
let MyCol = new Mongo . Collection ( 'mycol' ) ;
Meteor . connect ( 'wss://myapp.meteor.com/websocket' ) ; // Note the /websocket after your URL
class App extends React . Component {
render ( ) {
let { myThing } = this . props ;
return (
< View >
< Text > Here is the thing: { myThing . name } </ Text >
</ View >
) ;
}
}
let AppContainer = withTracker ( ( ) => {
Meteor . subscribe ( 'myThing' ) ;
let myThing = MyCol . findOne ( ) ;
return {
myThing ,
} ;
} ) ( App ) ;
export default AppContainer ;唯一方案:在物理设备上运行该应用,但要连接到本地开发机器?查看此问题评论。
@meteorrn/core软件包已尽可能轻巧。要访问更多功能,您可以使用伴侣软件包。
这里有一些例子:
@meteorrn/oauth-google :允许您让用户使用Google登录您的应用@meteorrn/oauth-facebook :允许您使用Facebook登录您的应用程序有关正式识别软件包的完整列表,请查看@meteorrn github org。
该软件包与从0.60.0到最新的React本机版本兼容(0.63.2)
对于反应天然<0.60.0,请使用反应现象。
从react-native-meteor中迁移:
MeteorListView和MeteorComplexListViewCollectionFS已被删除createContainer已被删除connectMeteor )已删除composeWithTracker已被删除虽然该软件包是考虑到React Nation的设计,但它也能够在Web上运行(使用react-dom )。如果您需要一个轻量级的流星实现,如果要与服务器代码库分开创建客户端应用程序,则这可能很有用。所需的唯一更改是提供异步实现。这是一个简单的例子:
const AsyncStorage = {
setItem : async ( key , value ) => window . localStorage . setItem ( key , value ) ,
getItem : async ( key ) => window . localStorage . getItem ( key )
removeItem : async ( key ) => window . localStorage . removeItem ( key )
}
Meteor . connect ( "wss://.../websock" , { AsyncStorage } ) ; github版本选项卡包括一个完整的变换仪
为了确保Meteorrn Companion软件包使用与核心相同的外部软件包(例如Asyncstorage)的相同版本, @meteorrn/core提供了一个软件包接口,配套软件包可以访问某些软件包。当前,软件包接口返回具有以下属性的对象:
import Meteor from '@meteorrn/core' ;
const { AsyncStorage } = Meteor . packageInterface ( ) ;observeChanges (但它确实实现了observe ) 该库包含几个内部类和构造,这些内部类和构造主要是自行运行的,并且没有用户的影响。
调试图书馆按预期工作需要听几个事件。以下显示了几个允许进行详细记录和检查的事件。
日志记录对于无法访问控制台的生产应用程序的分析和仪器很有用
跟踪内部内容的最方便方法是通过Data.onChange :
const Data = Meteor . getData ( ) ;
data . onChange ( ( event ) => console . debug ( event ) ) ;在引擎盖下确实如此:
this . db . on ( 'change' , cb ) ;
this . ddp . on ( 'connected' , cb ) ;
this . ddp . on ( 'disconnected' , cb ) ;
this . on ( 'loggingIn' , cb ) ;
this . on ( 'loggingOut' , cb ) ;
this . on ( 'change' , cb ) ;您也可以在loggingIn , loggingOut , onLogin , onLoginFailure上收听并单独change :
const Data = Meteor . getData ( ) ;
Data . on ( 'loggingIn' , ( e ) => console . debug ( 'loggingIn' , e ) ) ;
// ... const events = [
// connection messages
'connected' ,
'disconnected' ,
// Subscription messages (Meteor Publications)
'ready' ,
'nosub' ,
'added' ,
'changed' ,
'removed' ,
// Method messages (Meteor Methods)
'result' ,
'updated' ,
// Error messages
'error' ,
] ;
const Data = Meteor . getData ( ) ;
events . forEach ( ( eventName ) => {
Data . ddp . on ( eventName , ( event ) => console . debug ( eventName , event ) ) ;
} ) ; 图书馆试图使用反应式提供的本机Websocket。在以下事件中,您可以将服务器挂接到低级消息传递中:
open - Websocket成功打开close - Websocket成功关闭message:out消息发送到服务器message:in服务器传来的消息error - Websocket级别发生了错误 const Data = Meteor . getData ( ) ;
const socket = Data . ddp . socket ;
const events = [ 'open' , 'close' , 'message:out' , 'message:in' , 'error' ] ;
events . forEach ( ( eventName ) => {
socket . on ( eventName , ( event ) => console . debug ( eventName , event ) ) ;
} ) ; 有可能通过访问原始套接字来将Websocket挂入一个级别。
这是高度灰心的生产,自担风险!请注意,data.ddp.socket会聆听其中的一些(例如错误),并将它们召集起来,但还可以正确处理清理和垃圾收集。原始插座错误将使
isRaw属性设置为true。
const Data = Meteor . getData ( ) ;
const rawSocket = Data . ddp . socket . rawSocket ;
rawSocket . onopen = ( e ) => console . debug ( 'raw open' , e ) ;
rawSocket . onmessage = ( e ) => console . debug ( 'raw message' , e ) ;
rawSocket . onclose = ( e ) => console . debug ( 'raw close' , e ) ;
rawSocket . onerror = ( e ) => console . debug ( 'raw error' , e ) ; 您可以直接从Minimongo插入DB事件:
const Data = Meteor . getData ( ) ;
Data . db . on ( 'change' , ( e ) => console . debug ( e ) ) ;尽管Meteor依赖于WebSocket Connections和DDP作为协议,但您有时可能希望通过HTTP发送数据。
以下示例提供了一种轻松的方式来聆听错误并通过fetch请求将其发送到服务:
// in your App code
const errorToBody = ( err ) => {
const errProps = Object . getOwnPropertyNames ( err ) ;
const formBody = [ ] ;
for ( const prop of errProps ) {
const encodedKey = encodeURIComponent ( prop ) ;
const encodedValue = encodeURIComponent ( err [ prop ] ) ;
formBody . push ( encodedKey + '=' + encodedValue ) ;
}
return formBody . join ( '&' ) ;
} ;
const sendError = ( err ) => {
fetch ( 'https://mydomain.tld/log/error' , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8' ,
} ,
body : errToBody ( err ) ,
} )
. then ( console . debug )
. catch ( console . error ) ;
} ;
// hook into all DDP and socket-level errors
const Data = Meteor . getData ( ) ;
Data . dpp . on ( 'error' , ( e ) => {
const error = e instanceof Error ? e : e ?. error ;
return error && sendError ( error ) ;
} ) ; | whazzup.co | StarlingRealTime |
|---|---|
![]() | |
| whazzup.co在其本机应用中使用流星反应本地 | StarlingRealTime使用Meteor React在其生产应用中 |
| lea.online | 计划B Schule |
|---|---|
| Lea.Online使用Meteor React在其本机移动学习应用中 | 计划B Schule在其生产应用中使用流星反应本地 |
是否想使用流星反应本地展示您的应用?只需在此处创建PR即可添加您的徽标。
Meteor React由JanKüster维护,以前由Nathaniel Dsouza维护,他可以咨询:[email protected]
感谢对该项目的任何贡献!
如果您有问题,问题或想讨论问题,请使用我们的问题链接,这些链接将帮助您找到正确的询问或讲述的位置。
如果您想贡献代码,请确保您已经阅读了我们的贡献指南和我们的行为准则。
您可以随时询问我们,是否卡住或这些文件中的任何一个都不清楚。
麻省理工学院,请参阅许可证文件