-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: Combining Routes and RootContainer into one root component #966
Comments
This idea came up in internal discussion too. CC'ing some of the participants in case they want to respond: @devknoll, @yungsters, @cpojer, @joshduck, @sahrens, @josephsavona, @elynde. (If nobody chimes in, I'll try and summarize the thread later on.) |
Yeah, this bubbled up internally too in P56213804. @josephsavona suggested I toss it here. |
Thanks for moving the discussion here so the community can participate (this started as an internal discussion). Overall I agree that a higher-level component along the lines of your proposal could be useful in certain situations. This is a great example of something that should be possible (and not too difficult) to build in user-land after we've completed the Relay Core API in #559.
This is a common pattern (let's call it one-root-per-page), but it isn't the only approach to handling top-level views. Many apps use a router such that there is one fixed
Here too, some containers that are rendered at the top-level may be reused as sub-containers elsewhere and some may not. Requiring every container to implement handling for loading states and errors would introduce an unnecessary burden on developers - as you point out, there is something special about the root component. The existing APIs - That said, it's clear that a merged query config + data fetcher + root container style API could be quite useful. At this point most of what's necessary to implement this in user-land is exposed via the Note: I suspect you'll end up needing |
One additional thought: what if the API for the const StarWarsApp = createRootContainer(
// props are the data for each query
({error, props}) => {
if (error) {
return <Text>{error.message}</Text>;
}
return (
<View>
{props.viewer.factions.map(faction => <Faction key={faction.id} faction={faction} />)}
</View>
);
},
{
initialVariables: {
count: 10,
},
// no nested fragments
queries: {
viewer: () => Relay.QL`
query {
viewer {
factions(first: $count) {
id
${Faction.getFragment('faction')}
}
}
}
`,
}
}
);
React.render(
<StarWarsApp />,
...
); i.e., make this a higher-order component instead of sub-subclassing |
Our initial reasoning behind this was that we envisioned a single application component that would conditionally compose sub components. For example an This isn't incompatible with the initial suggestion. But it does mean that has to be aware of all possible root fields (which is currently the case) and when to fetch those fields and how to populate the properties of the fields. This becomes difficult when you consider that a root fields might have different meanings depending on the route. For example This could be overcome if you had a root component per route, e.g. |
@mroch has a lot of context on this, too. |
I'm marking this as "help wanted" - the general direction is promising, but this seems like a prime candidate for something that should be achievable given #559. The best step is probably for someone to try implementing it, figure out what public API is missing to support this and then build that API as part of #559. |
I can take a stab at it when I have time, after I come back from vacation :).
|
Because of the progress that's been made developing |
I'm curious what you guys think of combining
Routes
andRootContainer
into just one component.It often is the case that most Relay pages / apps have a top level element that handles the initial entry point of Relay. This top level element is typically always paired with a specific route, and requires a
RootContainer
orRelayRenderer
to work. As @ianstormtaylor has mentioned in #822, theRoutes
/RelayQueryConfig
concept is very lightweight and feels almost like a dangling object that needs to coexist with other things.I know that containers are meant to be composable anywhere, but given that there often is this top level element in practice, I was wondering if we can get rid of the complexity of these concepts and just have a root component that combines these together. The root component can also take care of handling error states, if they're passed in as props:
This goes a step short of making this enabled for all relay containers (so
StarWarsPageContainer
is still composable just like any other component), but introduces a distinction that it's possible to compose a top level element into an initial entry point for Relay. It also gets rid of a few concepts that I hope would be a simplification for everyone :).The text was updated successfully, but these errors were encountered: