2016-03-30 25 views
2

什麼是首選的方式,利用繼電器來傳遞數據字典, 比如我在接口什麼是最好的方式來傳遞數據字典,利用繼電器

UsersList = [ 
    { 
    userName 
    // each user has select control 
    CountrySelectControl { 
     value = Country 
     options = [All Countries List] 
    } 
] 

什麼是閱讀所有國家名單的正確方法? 據我瞭解不是查詢graphQl就像我看到我需要在根查詢countriesList這個

{ users { userName, country, countriesList } } 

所以唯一的辦法,並通過道具手動將它傳遞給每一個孩子組成一個好主意?

class Blabla extends Relay.Route { 
    static queries = { 
    users: (Component) => Relay.QL` 
     query UsersQuery { 
     users { ${Component.getFragment('user')} }, 
     } 
    `, 
    countriesList: (Component) => Relay.QL` 
     query countriesListQuery { 
     countriesList { ${Component.getFragment('countriesList')} }, 
     } 
    `, 
... 
} 

如果我有很多字典和一些更深的UI結構,這成爲一個痛苦。

或者我可以以某種方式在樹中更深地傳遞根數據,而不必將這些數據寫入道具中。 (我的意思是沒有上下文)

回答

0

是的,你可以在樹中傳遞更深的根數據,而不需要明確地寫作countryList作爲道具。

假設我們有一個大陸和屬於它的國家的數據。我們嵌套了UI組件。例如,ContinentComponent包括CountryListComponent,它需要一個國家列表。 A CountryListComponent由若干CountryComponent組成,它需要一個狀態列表。我們可以利用高層次的道具,而不是讓ContinentComponent通過國家列表和狀態列表,一直到CountryListComponentCountryComponent

我們可以指定高層在高級別組件ContinentComponent支撐continent如下:

export default Relay.createContainer(ContinentComponent, { 
    fragments: { 
    continent:() => Relay.QL` 
     fragment on Continent { 
     ${CountryListComponent.getFragment('continent')}, 
     } 
    `, 
    }, 
}); 

代替country list道具,只有道具continent傳遞給CountryListComponent從ContinentComponent。

接下來,我們在CountryListComponent指定必要的道具:

export default Relay.createContainer(CountryListComponent, { 
    fragments: { 
    continent:() => Relay.QL` 
     fragment on Continent { 
     countryList(first: 100) { 
      edges { 
      node { 
       id, 
       name, 
      }, 
      ${CountryComponent.getFragment('country')}, 
      }, 
     }, 
     } 
    `, 
    }, 
}); 

現在,CountryListComponent特定的道具價值this.props.continent.countryList.edges[index].node傳遞給CountryComponent。

這個用例是Relay.js的主要動機之一。

+0

您仍然需要將此大陸根道具一直傳遞到您需要它的React組件。我想明確表明用戶使用CountryList的主要想法,但如果在UserList中使用此列表,我需要爲每個創建用戶傳遞大量重複項。 –

+0

@ivanstarkov,關於根道具,這是不正確的。例如,'CountryComponent'只需要傳遞'country' prop,如果它不需要root prop下的其他東西。關於'countryList' prop,它不再顯式傳遞給樹中的每個級別。無論如何,對於你的問題,國家名單是不同的用戶不同? –

相關問題