
將您的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]
感謝對該項目的任何貢獻!
如果您有問題,問題或想討論問題,請使用我們的問題鏈接,這些鏈接將幫助您找到正確的詢問或講述的位置。
如果您想貢獻代碼,請確保您已經閱讀了我們的貢獻指南和我們的行為準則。
您可以隨時詢問我們,是否卡住或這些文件中的任何一個都不清楚。
麻省理工學院,請參閱許可證文件