question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] New Dependency: `relay-hooks`

See original GitHub issue

New Dependency

Name: relay-hooks

URL: https://github.com/relay-tools/relay-hooks

Motivation

We use relay with react-relay already, and it works great. We use createFragmentContainer and QueryRenderer to make our components.

We already use hooks if a bunch of places and we are moving towards them more and more with new code.

relay-hooks is the bridge of these two “worlds”. It provides hooks like useFragment and useQuery that replace the functions I mentioned earlier. This makes our components cleaner. We create a component and use hooks to connect it with relay, rather than having relay create and manage all that for us.

Look at this:

export const ViewingRoomQueryRenderer: React.FC<{ viewingRoomID: string }> = ({ viewingRoomID }) => {
  return (
    <QueryRenderer<ViewingRoomQuery>
      environment={defaultEnvironment}
      query={graphql`
        query ViewingRoomQuery($viewingRoomID: ID!) {
          viewingRoom(id: $viewingRoomID) {
            ...ViewingRoom_viewingRoom
          }
        }
      `}
      cacheConfig={{ force: true }}
      variables={{
        viewingRoomID,
      }}
      render={renderWithLoadProgress(ViewingRoomFragmentContainer)}
    />
  )
}

where our component is basically a relay component that we customize and adjust using the props.

Now look at this:

const query = graphql`
  query ViewingRoomQuery($viewingRoomID: ID!) {
    viewingRoom(id: $viewingRoomID) {
      ...ViewingRoom_viewingRoom
    }
  }
`

export const ViewingRoomQueryRenderer: React.FC<{ viewingRoomID: string }> = ({ viewingRoomID }) => {
  const { props, error } = useQuery(query, { viewingRoomID }, { networkCacheConfig: { force: true } })


  if (props && props.viewingRoom) {
    return <ViewingRoomFragmentContainer viewingRoom={props.viewingRoom} />
  }
  if (error) {
    throw error
  }

  return <LoadingScreen />
}

where our component is actually our code, and we control the flow from what we get from the hook.

The biggest changes here are that environment={defaultEnvironment} is not part of the component any more, it is now a Context from RelayEnvironmentProvider that we add when registering the screens here, and that we don’t need to use the render prop to have loading animations and placeholders, we can now just render the actual loading or placeholder components, so instead of render={renderWithLoadProgress(ViewingRoomFragmentContainer)} we can just use return <LoadingScreen /> at the bottom of our component.

Of course, adding this dependency does not mean we will rewrite all the relay components to use hooks, but it makes it possible to start using the relay hooks.

Check List

  • Have you read over the source code?
  • Has had a release in the last year, or looks done and stable?
  • Could you fit this codebase in your head after reading the source?
  • Is this the stand-out obvious answer to a particular domain problem?
  • Do you expect your team to be the only people who know about this dependency?
  • Is this obviously being used in production by the maintainers? Is it battle-tested?
  • Does our bundle already include a (transitive) dependency that solves the problem and could we use that instead?
  • Do you feel well versed in the domain of this dependency and/or could you maintain it if that needs to become an option?

Alternatives

N/A

Notes

This was already added in this PR while I was trying to make pagination work for the Viewing Room list, and it made my life easier. I added it as a test and it ended up helping. You can see it used here for useQuery and here for usePagination.

I’m sorry for making the RFC after the fact.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
pviniscommented, Jul 14, 2020

We did try it out with matt, importing from react-relay/hooks. Some were missing, some args were different, and most places seem to suggest relay-hooks is the package to go, until they are officially provided.

0reactions
pviniscommented, Jul 21, 2020

Resolution

We agree to add it. Thanks everyone for the feedback.

Level of Support

2: Positive feedback.

Next Steps

It’s already added.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing Relay Hooks
Relay Hooks is a set of new, rethought APIs for fetching and managing GraphQL data using React Hooks. The new APIs are fully...
Read more >
تويتر \ Lorenzo Di Giacomo (m0rrys@) - Twitter
Creator of Relay Hooks, React Relay Offline, Relay Angular & Wora Solution Architect of enterprise applications in cloud native and j2ee environments.
Read more >
What you need to know about the React useEvent Hook RFC
The useEvent Hook returns a function wrapped in the useCallback Hook with an empty dependency array [] . This is why the function...
Read more >
Overview - Joe Sandbox
Number of analysed new started processes analysed: 10 ... fetchQuery;function a(a,c,d){i();var e=b("react-relay/relay-hooks/useRelayEnvironment")(),f.
Read more >
relay-hooks - npm
relay-hooks. TypeScript icon, indicating that this package has built-in type declarations · Readme · Code Beta · 2 Dependencies · 12 Dependents ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found