2016-10-15 25 views
3

我使用Meteor的Apollo客戶端與wix/react-native-navigation一起使用,我想知道是否可以將由Navigation.startSingleScreenApp創建的根組件包裝在ApolloProvider組件?我試過把Navigation.startSingleScreenApp放在另一個組件的渲染方法中,並把它包含在ApolloProvider中,但那不起作用。有沒有人有過使用這兩者的經驗?我可以將由wix/react-native-navigation創建的根組件包裝在ApolloProvider組件中

我在想,如果每個註冊了Navigation.registerComponent的組件都有自己獨立的根組件,並且如果是這種情況,我必須將我註冊的每個組件都附上ApolloProvider,可能使用更高階的組件?我會很感激任何人可以提供的想法或示例代碼!提前致謝。

回答

1

我已經用包裝每個註冊組件使用更高階的組件。我用於HOC的代碼如下:

import React from 'react'; 
import appClient from '../store/apollo'; 
import { ApolloProvider } from 'react-apollo'; 

export default function apolloProviderHOC(WrappedComponent){ 
    return class PP extends React.Component { 
    render() { 
     return (
     <ApolloProvider client={appClient}> 
      <WrappedComponent {...this.props}/> 
     </ApolloProvider> 
    ); 
    } 
    } 
} 

只需導入上面的函數並將要包裝的組件傳遞給它。

0

我發現這個問題的最簡單的解決方法是簡單地傳遞ApolloClient在registerComponent功能類似:

Navigation.registerComponent('pm.SplashScreen',() => SplashScreen, store, Provider, {client}); 

的ApolloClient已被稱爲「客戶端」,因爲反應本地導航採用的是傳播您通過該對象的運算符。

相關問題