Skip to content
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

Add separate JavaScript / TypeScript example tabs for Cells documentation #11743

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions docs/docs/cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,42 @@ export const beforeQuery = (props) => {

For example, if you wanted to turn on Apollo's polling option, and prevent caching, you could export something like this (see Apollo's docs on [polling](https://www.apollographql.com/docs/react/data/queries/#polling) and [caching](https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy))

<Tabs groupId="js-ts">
<TabItem value="js" label="JavaScript">

```jsx
export const beforeQuery = (props) => {
return { variables: props, fetchPolicy: 'no-cache', pollInterval: 2500 }
return {
variables: props,
Philzen marked this conversation as resolved.
Show resolved Hide resolved
fetchPolicy: 'no-cache',
pollInterval: 2500
}
}
```

</TabItem>
<TabItem value="ts" label="TypeScript">

```jsx
export const beforeQuery = (
props
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can also provide suggested types for props here?

): GraphQLQueryHookOptions<FindBlogPostQuery, FindBlogPostQueryVariables> => {
return {
variables: props,
fetchPolicy: 'no-cache',
pollInterval: 2500
}
}
```

</TabItem>
</Tabs>

You can also use `beforeQuery` to populate variables with data not included in the Cell's props (like from React's Context API or a global state management library). If you provide a `beforeQuery` function, the Cell will automatically change the type of its props to match the first argument of the function.

<Tabs groupId="js-ts">
<TabItem value="js" label="JavaScript">

```jsx
// The Cell will take no props: <Cell />
export const beforeQuery = () => {
Expand All @@ -162,15 +190,54 @@ export const beforeQuery = () => {
}
```

</TabItem>
<TabItem value="ts" label="TypeScript">

```jsx
// The Cell will take no props: <Cell />
export const beforeQuery = (): GraphQLQueryHookOptions<
ArticlesQuery, ArticlesQueryVariables
> => {
const { currentUser } = useAuth()

return {
variables: { userId: currentUser.id },
}
}
```

</TabItem>
</Tabs>

<Tabs groupId="js-ts">
<TabItem value="js" label="JavaScript">

```jsx
// The cell will take 1 prop named "word" that is a string: <Cell word="abc">
export const beforeQuery = ({ word }: { word: string }) => {
export const beforeQuery = ({ word }) => {
return {
variables: { magicWord: word },
}
}
```

</TabItem>
<TabItem value="ts" label="TypeScript">

```jsx
// The cell will take 1 prop named "word" that is a string: <Cell word="abc">
export const beforeQuery = (
{ word }: { word: string }
): GraphQLQueryHookOptions<FindBlogPostQuery, FindBlogPostQueryVariables> => {
return {
variables: { magicWord: word }
}
}
```

</TabItem>
</Tabs>

### isEmpty

`isEmpty` is an optional lifecycle hook. It returns a boolean to indicate if the Cell should render empty. Use it to override the default check, which checks if the Cell's root fields are null or empty arrays.
Expand Down
Loading