[英]Conditional fragments or embedded root-containers when using Relay with React-Native
I've got relay
working with react-native
, but I'm confused about how to best utilize relay routes and root containers, especially when working with a Navigator
that renders multiple routes. 我已经使用
react-native
了relay
,但是我对如何最好地利用中继路由和根容器感到困惑,尤其是在使用呈现多个路由的Navigator
。
Take the following class: 参加以下课程:
var Nav = React.createClass({
renderScene(route, nav) {
switch(route.id) {
case 'first':
return <First navigator={nav} />
case 'second':
return <Second navigator={nav} />
}
},
render() {
<Navigator
initialRoute={{ id: 'first' }}
renderScene={this.renderScene}
/>
}
})
module.exports = Relay.createContainer(Nav, {
fragments: {
viewer: () => Relay.QL`
fragment on User {
${First.getFragment('viewer'},
${Second.getFragment('viewer'}
}
`
}
})
In my parent route, I then request the User
fragment which builds the query. 在我的父路由中,然后我请求构建查询的
User
片段。
The problem is that the fragment will be including those fields defined by both the first
and second
components, even if only one of them is being displayed at a time. 问题是片段将包括由
first
和second
组件定义的那些字段,即使一次只显示其中一个。
In this case should I: 在这种情况下我应该:
1) return another Relay.RootContainer
in the renderScene
callback? 1)在
renderScene
回调中返回另一个Relay.RootContainer
? Is it generally a bad idea to embed Relay.RootContainers
within each-other? 在每个其他内部嵌入
Relay.RootContainers
通常是个坏主意吗?
renderScene(route, nav) {
switch(route.id) {
case 'first':
return (
<Relay.RootContainer
Component={First}
route={new FirstRoute()}
renderFetched={(data) => {
return <First navigator={nav} {...data} />
}}
/>
)
}
}
2) Use conditional variables to include a fragment? 2)使用条件变量来包含片段?
initialVariables: {
first: true
},
fragments: {
viewer: (variables) => Relay.QL`
fragment on User {
${First.getFragment('viewer').if(variables.first)
}
`
}
Or are there other suggestions? 或者还有其他建议吗?
Using something like this: 1 使用这样的东西: 1
function matchRoute(route, map) {
return map[route.name] ? map[route.name]() : null;
}
Try this: 试试这个:
fragments: {
viewer: (variables) => Relay.QL`
fragment on User {
${route => matchRoute(route, {
FirstRoute: () => First.getFragment('viewer'),
SecondRoute: () => Second.getFragment('viewer'),
}},
}
`,
}
[1]: medium.com/@cpojer/relay-and-routing [1]: medium.com/@cpojer/relay-and-routing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.