Part 6 - Fragments
This is part 6 of series
This part is based on the Fragments section of this tutorial
useFragment
After all the work we have done so far, following Fragments section in Relay tutorial is a breeze.
Commit: https://github.com/tuan/newsfeed-demo-app/commit/b3ce37e71a9007a9cd717ebd06d6cc5efa78ee64
There are a few things related to fragment that might be beneficial if you’re new to Relay:
- Think of the Relay query as a tree: each fragment is a node. This Relay tree is a mirror of React component tree. With this in mind, when you re-use your component in an existing part of the component tree, you should also re-use, i.e. spread, your component’s fragment in the corresponding part of the Relay tree.
- Use the following naming convention when naming your fragment: ComponentNameFragment_componentPropName. Here’s an example in Image.tsx:
const ImageFragment = graphql` fragment ImageFragment_image on Image { url } `; - The type of your fragment key always has the suffix
$key - Your fragment key is the nearest parent node in the Relay query where you spread your fragment
After making the changes, you should now see the timestamp rendered correctly