Skip to content

Commit 73291ee

Browse files
lynnshaoyufacebook-github-bot
authored andcommitted
v20.1.0
Reviewed By: evanyeung Differential Revision: D79129481 fbshipit-source-id: 4b5fb017b4e8ba7101bf80323aaa0e301077dfcb
1 parent 9139dca commit 73291ee

133 files changed

Lines changed: 20958 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
id: entrypoint-container
3+
title: EntryPointContainer
4+
slug: /api-reference/entrypoint-container/
5+
description: API reference for EntryPointContainer, a React component used to render the root component of an entrypoint
6+
keywords:
7+
- entrypoint
8+
- container
9+
- root
10+
---
11+
12+
import DocsRating from '@site/src/core/DocsRating';
13+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
14+
15+
## `EntryPointContainer`
16+
17+
<FbInternalOnly>
18+
19+
For more information, see the [Defining EntryPoints](../../guides/entrypoints/using-entrypoints/#defining-entrypoints) and [Consuming EntryPoints](../../guides/entrypoints/using-entrypoints/#-entrypoints) guides.
20+
21+
</FbInternalOnly>
22+
23+
```js
24+
function EntryPointContainer({
25+
entryPointReference,
26+
props,
27+
}: {
28+
+entryPointReference: PreloadedEntryPoint<TEntryPointComponent>,
29+
+props: TRuntimeProps,
30+
}): ReactElement
31+
```
32+
33+
A React component that renders a preloaded EntryPoint.
34+
35+
* `entryPointReference`: the value returned from a call to `loadEntryPoint` or acquired from the `useEntryPointLoader` hook.
36+
* `props`: additional runtime props that will be passed to the `Component`
37+
38+
<DocsRating />
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
id: load-entrypoint
3+
title: loadEntryPoint
4+
slug: /api-reference/load-entrypoint/
5+
description: API reference for loadEntryPoint, which imperatively loads an entrypoint and data for its queries
6+
keywords:
7+
- entrypoint
8+
- preload
9+
- render-as-you-fetch
10+
---
11+
12+
import DocsRating from '@site/src/core/DocsRating';
13+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
14+
15+
## `loadEntryPoint`
16+
17+
This function is designed to be used with `EntryPointContainer` to implement the "render-as-you-fetch" pattern.
18+
19+
EntryPoint references returned from `loadEntryPoint` will leak data to the Relay store (if they have associated queries) unless `.dispose()` is called on them once they are no longer referenced. As such, prefer using `useEntryPointLoader` when possible, which ensures that EntryPoint references are correctly disposed for you. See the [`useEntryPointLoader`](../use-entrypoint-loader) docs for a more complete example.
20+
21+
<FbInternalOnly>
22+
23+
For more information, see the [Loading EntryPoints](../../guides/entrypoints/using-entrypoints/#loading-entrypoints) guide.
24+
25+
</FbInternalOnly>
26+
27+
```js
28+
const EntryPoint = require('MyComponent.entrypoint.js');
29+
30+
const {loadQuery} = require('react-relay');
31+
32+
// Generally, your component should access the environment from the React context,
33+
// and pass that environment to this function.
34+
const getEntrypointReference = environment => loadEntryPoint(
35+
{ getEnvironment: () => environment },
36+
EntryPoint,
37+
{id: '4'},
38+
);
39+
40+
// later: pass entryPointReference to EntryPointContainer
41+
// Note that EntryPoint references should have .dispose() called on them,
42+
// which is missing in this example.
43+
```
44+
45+
### Arguments
46+
47+
* `environmentProvider`: A provider for a Relay Environment instance on which to execute the request. If you're starting this request somewhere within a React component, you probably want to use the environment you obtain from using [`useRelayEnvironment`](../use-relay-environment/).
48+
* `EntryPoint`: EntryPoint to load.
49+
* `entryPointParams`: Parameters that will be passed to the EntryPoint's `getPreloadProps` method.
50+
51+
### Flow Type Parameters
52+
53+
* `TEntryPointParams`: Type parameter corresponding to the type of the first parameter of the `getPreloadProps` method of the EntryPoint.
54+
* `TPreloadedQueries`: the type of the `queries` parameter to the EntryPoint component.
55+
* `TPreloadedEntryPoints`: the type of the `entrypoints` parameter passed to the EntryPoint component.
56+
* `TRuntimeProps`: the type of the `props` prop passed to `EntryPointContainer`. This object is passed down to the EntryPoint component, also as `props`.
57+
* `TExtraProps`: if an EntryPoint's `getPreloadProps` method returns an object with an `extraProps` property, those extra props will be passed to the EntryPoint component as `extraProps`.
58+
* `TEntryPointComponent`: the type of the EntryPoint.
59+
* `TEntryPoint`: the type of the EntryPoint.
60+
61+
### Return Value
62+
63+
An EntryPoint reference with the following properties:
64+
65+
* `dispose`: a method that will release any query references loaded by this EntryPoint (including indirectly, by way of other EntryPoints) from being retained by the store. This can cause the data referenced by these query reference to be garbage collected.
66+
67+
The exact format of the return value is *unstable and highly likely to change*. We strongly recommend not using any other properties of the return value, as such code would be highly likely to break when upgrading to future versions of Relay. Instead, pass the result of `loadEntryPoint()` to `EntryPointContainer`.
68+
69+
### Behavior
70+
71+
* When `loadEntryPoint()` is called, each of an EntryPoint's associated queries (if it has any) will load their query data and query AST. Once both the query AST and the data are available, the data will be written to the store. This differs from the behavior of `prepareEntryPoint_DEPRECATED`, which would only write the data from an associated query to the store when that query was rendered with `usePreloadedQuery`.
72+
* The EntryPoint reference's associated query references will be retained by the Relay store, preventing it the data from being garbage collected. Once you call `.dispose()` on the EntryPoint reference, the data from the associated queries is liable to be garbage collected.
73+
* `loadEntryPoint` may throw an error if it is called during React's render phase.
74+
75+
76+
77+
<DocsRating />
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
id: use-entrypoint-loader
3+
title: useEntryPointLoader
4+
slug: /api-reference/use-entrypoint-loader/
5+
description: API reference for useEntryPointLoader, a React hook used to load entrypoints in response to user events
6+
keywords:
7+
- render-as-you-fetch
8+
- entrypoint
9+
- preload
10+
---
11+
12+
import DocsRating from '@site/src/core/DocsRating';
13+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
14+
15+
## `useEntryPointLoader`
16+
17+
Hook used to make it easy to safely work with EntryPoints, while avoiding data leaking into the Relay store. It will keep an EntryPoint reference in state, and dispose of it when it is no longer accessible via state.
18+
19+
<FbInternalOnly>
20+
21+
For more information, see the [Loading EntryPoints](https://www.internalfb.com/intern/wiki/Relay/Guides/entry-points/#loading-entrypoints) guide.
22+
23+
</FbInternalOnly>
24+
25+
```js
26+
const {useEntryPointLoader, EntryPointContainer} = require('react-relay');
27+
28+
const ComponentEntryPoint = require('Component.entrypoint');
29+
30+
function EntryPointRevealer(): React.MixedElement {
31+
const environmentProvider = useMyEnvironmentProvider();
32+
const [
33+
entryPointReference,
34+
loadEntryPoint,
35+
disposeEntryPoint,
36+
] = useEntryPointLoader(environmentProvider, ComponentEntryPoint);
37+
38+
return (
39+
<>
40+
{
41+
entryPointReference == null && (
42+
<Button onClick={() => loadEntryPoint({})}>
43+
Click to reveal the contents of the EntryPoint
44+
</Button>
45+
)
46+
}
47+
{
48+
entryPointReference != null && (
49+
<>
50+
<Button onClick={disposeEntryPoint}>
51+
Click to hide and dispose the EntryPoint.
52+
</Button>
53+
<Suspense fallback="Loading...">
54+
<EntryPointContainer
55+
entryPointReference={entryPointReference}
56+
props={{}}
57+
/>
58+
</Suspense>
59+
</>
60+
)
61+
}
62+
</>
63+
);
64+
}
65+
```
66+
67+
### Arguments
68+
69+
* `environmentProvider`: an object with a `getEnvironment` method that returns a Relay environment.
70+
* `EntryPoint`: the EntryPoint, usually acquired by importing a `.entrypoint.js` file.
71+
72+
### Flow Type Parameters
73+
74+
* `TEntryPointParams`: the type of the first argument to the `getPreloadProps` method of the EntryPoint.
75+
* `TPreloadedQueries`: the type of the `queries` prop passed to the EntryPoint component.
76+
* `TPreloadedEntryPoints`: the type of the `entryPoints` prop passed to the EntryPoint component.
77+
* `TRuntimeProps`: the type of the `props` prop passed to `EntryPointContainer`. This object is passed down to the EntryPoint component, also as `props`.
78+
* `TExtraProps`: if an EntryPoint's `getPreloadProps` method returns an object with an `extraProps` property, those extra props will be passed to the EntryPoint component as `extraProps` and have type `TExtraProps`.
79+
* `TEntryPointComponent`: the type of the EntryPoint component.
80+
* `TEntryPoint`: the type of the EntryPoint.
81+
82+
### Return value
83+
84+
A tuple containing the following values:
85+
86+
* `entryPointReference`: the EntryPoint reference, or `null`.
87+
* `loadEntryPoint`: a callback that, when executed, will load an EntryPoint, which will be accessible as `entryPointReference`. If a previous EntryPoint was loaded, it will dispose of it. It may throw an error if called during React's render phase.
88+
* Parameters
89+
* `params: TEntryPointParams`: the params passed to the EntryPoint's `getPreloadProps` method.
90+
* `disposeEntryPoint`: a callback that, when executed, will set `entryPointReference` to `null` and call `.dispose()` on it. It has type `() => void`. It should not be called during React's render phase.
91+
92+
### Behavior
93+
94+
* When the `loadEntryPoint` callback is called, each of an EntryPoint's associated queries (if it has any) will load their query data and query AST. Once both the query AST and the data are available, the data will be written to the store. This differs from the behavior of `prepareEntryPoint_DEPRECATED`, which would only write the data from an associated query to the store when that query was rendered with `usePreloadedQuery`.
95+
* The EntryPoint reference's associated query references will be retained by the Relay store, preventing it the data from being garbage collected. Once you call `.dispose()` on the EntryPoint reference, the data from the associated queries is liable to be garbage collected.
96+
* The `loadEntryPoint` callback may throw an error if it is called during React's render phase.
97+
98+
99+
<DocsRating />

0 commit comments

Comments
 (0)