I have a common Header
and Footer
that displays data that it may be already in the relay store and the main PageComponent
that expects data from the “query”, but I don’t want to display a full loading screen to the user, I want to display the Header, Footer and the loading screen or content in the middle.
So, as you know, if I wrap everything inside a QueryRenderer
, I will have two options: either the query is “loading” or the data is available:
<QueryRenderer
{...usualProps}
render={({ error, props }) => {
<div className="app-wrapper">
{ props || error ? <PageComponent {...props} error={error} /> : <div>Loading...</div>}
</div>
}}
/>
Let’s say I have a way to retrieve data from the store manually (as we don’t have something like partial rendering from the store) and I can pass that information to the common Header
and Footer
while the main query is loading:
<QueryRenderer
{...usualProps}
render={({ error, props }) => {
<div className="app-wrapper">
<Header {...storeProps} {...props} />
{ props || error ? <PageComponent {...props} error={error} /> : <div>Loading...</div>}
<Footer {...storeProps} {...props} />
</div>
}}
/>
I found that since I have the environment, I can do something like this to get the information from the store:
const storeProps = relayEnvironment.getStore().lookup({
dataID: 'client:root',
node: query().fragment, // special query with only the information I need for the `Header`
variables: {}
});
But then I have a few problems:
- QueryRenderer disposes the data when is unmounted :cry: (although the information I read from it should be retrieved by the Query too, reading from the store is more like a fallback while the query returns)
- The syntax looks hacky
- I have to create a query and compile it (I can live with this anyway)
Probably I’m aproaching the problem in the wrong way and maybe someone did something similar and can throw me a few pointers.
(In case nothing works my plan B is to use getDerivedStateFromProp
to send query information to custom context and save it there to avoid the store GC problem, for example).
TL:DR: I want to load data from the store while the query finishes loading and then use the data returned from the query.
Any ideas? Let me know if something is not clear