By now we are able to authenticate the user and retrieve her GitHub information, so we can start creating more screens to display the data. First, let's add the React Navigation library to handle navigation in the app:
yarnaddreact-navigation@^2.2.5
Now let's create a Navigation.js file in src/components, with the following contents:
We are also going to use Relay's QueryRenderer component and the loading logic for more screens, so let's extract the logic from HomeScreen to a new file, ScreenRenderer.js in src/components, with the following contents:
As you can see, we add a bit of logic in the QueryError component in order to apply a specific logic when the error is that the query couldn't be fetched. This happens when the app doesn't have network access, so we add a listener that will trigger the query to be retried when the app gets connected. We also display a different UI to the user, without the "retry" button that would fail anyways.
The ScreenRenderer in itself simply renders Relay's QueryRenderer using the environment it gets from the context, so that components using ScreenRenderer don't need to care about it. These components will need to provide the GraphQL query and the container component to render, as well as the navigation if needed by the container, and the variables used by the query. The environment is also injected in case it is needed by the container component, for example for mutations.
We'll also update the styles.js file to add an extra styles we'll use for navigation:
Let's go through a few of the changes, starting with the HomeScreen component: we add the static navigationOptions object with a title property, that will be used by the navigation to display the header, and you can notice the query provided to ScreenRenderer doesn't define all the data requirements itself anymore, but rather use the HomeScreen viewer fragment. This is a fundamental concept of Relay: data requirements are collocated to the components needing the data, which make them very easy to maintain. As you can see in this case, the HomeScreen query will contain the HomeScreen viewer fragment used by the HomeContainer component, and this fragment itself will contain the HomeScreen repository fragment used by the RepositoryItem component. Relay's compiler will resolve all these fragments so that the query satisfies all these data requirements.
The RepositoryItem has an onPress handler that will cause the app to navigate to the Repository screen, providing the repository's id and name values, that we will now use by creating the RepositoryScreen.js file in src/components, with the following contents:
This RepositoryScreen is a bit different from the HomeScreen because it uses dynamic parameters injected by the navigation: the title displayed in the header is the name of the repository, and the query needs the id of the repository node to fetch its data, here injected in the variables.
If you are not already running the Relay compiler in watch mode (using yarn run relay-watch) now is a good time to run it once to update the generated files, using yarn run relay-compile.
Last step is simply to add this screen to the Navigation.js file, the same way it already supports the HomeScreen:
That's it for this chapter! We can now list the 10 repositories last updated and navigate to a separate screen to display details about this repository, as we're going to implement in the next chapter.