UNPKG

8.65 kBTypeScriptView Raw
1/**
2 * These are types for things that are present in the upcoming React 18 release.
3 *
4 * Once React 18 is released they can just be moved to the main index file.
5 *
6 * To load the types declared here in an actual project, there are three ways. The easiest one,
7 * if your `tsconfig.json` already has a `"types"` array in the `"compilerOptions"` section,
8 * is to add `"react/next"` to the `"types"` array.
9 *
10 * Alternatively, a specific import syntax can to be used from a typescript file.
11 * This module does not exist in reality, which is why the {} is important:
12 *
13 * ```ts
14 * import {} from 'react/next'
15 * ```
16 *
17 * It is also possible to include it through a triple-slash reference:
18 *
19 * ```ts
20 * /// <reference types="react/next" />
21 * ```
22 *
23 * Either the import or the reference only needs to appear once, anywhere in the project.
24 */
25
26// See https://github.com/facebook/react/blob/master/packages/react/src/React.js to see how the exports are declared,
27
28import React = require('.');
29
30export {};
31
32declare const UNDEFINED_VOID_ONLY: unique symbol;
33type VoidOrUndefinedOnly = void | { [UNDEFINED_VOID_ONLY]: never };
34
35declare module '.' {
36 export interface SuspenseProps {
37 /**
38 * The presence of this prop indicates that the content is computationally expensive to render.
39 * In other words, the tree is CPU bound and not I/O bound (e.g. due to fetching data).
40 * @see {@link https://github.com/facebook/react/pull/19936}
41 */
42 unstable_expectedLoadTime?: number | undefined;
43 }
44
45 export type SuspenseListRevealOrder = 'forwards' | 'backwards' | 'together';
46 export type SuspenseListTailMode = 'collapsed' | 'hidden';
47
48 export interface SuspenseListCommonProps {
49 /**
50 * Note that SuspenseList require more than one child;
51 * it is a runtime warning to provide only a single child.
52 *
53 * It does, however, allow those children to be wrapped inside a single
54 * level of `<React.Fragment>`.
55 */
56 children: ReactElement | Iterable<ReactElement>;
57 }
58
59 interface DirectionalSuspenseListProps extends SuspenseListCommonProps {
60 /**
61 * Defines the order in which the `SuspenseList` children should be revealed.
62 */
63 revealOrder: 'forwards' | 'backwards';
64 /**
65 * Dictates how unloaded items in a SuspenseList is shown.
66 *
67 * - By default, `SuspenseList` will show all fallbacks in the list.
68 * - `collapsed` shows only the next fallback in the list.
69 * - `hidden` doesn’t show any unloaded items.
70 */
71 tail?: SuspenseListTailMode | undefined;
72 }
73
74 interface NonDirectionalSuspenseListProps extends SuspenseListCommonProps {
75 /**
76 * Defines the order in which the `SuspenseList` children should be revealed.
77 */
78 revealOrder?: Exclude<SuspenseListRevealOrder, DirectionalSuspenseListProps['revealOrder']> | undefined;
79 /**
80 * The tail property is invalid when not using the `forwards` or `backwards` reveal orders.
81 */
82 tail?: never | undefined;
83 }
84
85 export type SuspenseListProps = DirectionalSuspenseListProps | NonDirectionalSuspenseListProps;
86
87 /**
88 * `SuspenseList` helps coordinate many components that can suspend by orchestrating the order
89 * in which these components are revealed to the user.
90 *
91 * When multiple components need to fetch data, this data may arrive in an unpredictable order.
92 * However, if you wrap these items in a `SuspenseList`, React will not show an item in the list
93 * until previous items have been displayed (this behavior is adjustable).
94 *
95 * @see https://reactjs.org/docs/concurrent-mode-reference.html#suspenselist
96 * @see https://reactjs.org/docs/concurrent-mode-patterns.html#suspenselist
97 */
98 export const SuspenseList: ExoticComponent<SuspenseListProps>;
99
100 // must be synchronous
101 export type TransitionFunction = () => VoidOrUndefinedOnly;
102 // strange definition to allow vscode to show documentation on the invocation
103 export interface TransitionStartFunction {
104 /**
105 * State updates caused inside the callback are allowed to be deferred.
106 *
107 * **If some state update causes a component to suspend, that state update should be wrapped in a transition.**
108 *
109 * @param callback A _synchronous_ function which causes state updates that can be deferred.
110 */
111 (callback: TransitionFunction): void;
112 }
113
114 /**
115 * Returns a deferred version of the value that may “lag behind” it for at most `timeoutMs`.
116 *
117 * This is commonly used to keep the interface responsive when you have something that renders immediately
118 * based on user input and something that needs to wait for a data fetch.
119 *
120 * A good example of this is a text input.
121 *
122 * @param value The value that is going to be deferred
123 *
124 * @see https://reactjs.org/docs/concurrent-mode-reference.html#usedeferredvalue
125 */
126 export function useDeferredValue<T>(value: T): T;
127
128 /**
129 * Allows components to avoid undesirable loading states by waiting for content to load
130 * before transitioning to the next screen. It also allows components to defer slower,
131 * data fetching updates until subsequent renders so that more crucial updates can be
132 * rendered immediately.
133 *
134 * The `useTransition` hook returns two values in an array.
135 *
136 * The first is a boolean, React’s way of informing us whether we’re waiting for the transition to finish.
137 * The second is a function that takes a callback. We can use it to tell React which state we want to defer.
138 *
139 * **If some state update causes a component to suspend, that state update should be wrapped in a transition.**
140 *
141 * @param config An optional object with `timeoutMs`
142 *
143 * @see https://reactjs.org/docs/concurrent-mode-reference.html#usetransition
144 */
145 export function useTransition(): [boolean, TransitionStartFunction];
146
147 /**
148 * Similar to `useTransition` but allows uses where hooks are not available.
149 *
150 * @param callback A _synchronous_ function which causes state updates that can be deferred.
151 */
152 export function startTransition(scope: TransitionFunction): void;
153
154 const opaqueIdentifierBranding: unique symbol;
155 /**
156 * WARNING: Don't use this as a `string`.
157 *
158 * This is an opaque type that is not supposed to type-check structurally.
159 * It is only valid if returned from React methods and passed to React e.g. `<button aria-labelledby={opaqueIdentifier} />`
160 */
161 // We can't create a type that would be rejected for string concatenation or `.toString()` calls.
162 // So in order to not have to add `string | OpaqueIdentifier` to every react-dom host prop we intersect it with `string`.
163 type OpaqueIdentifier = string & {
164 readonly [opaqueIdentifierBranding]: unknown;
165 // While this would cause `const stringified: string = opaqueIdentifier.toString()` to not type-check it also adds completions while typing.
166 // It would also still allow string concatenation.
167 // Unsure which is better. Not type-checking or not suggesting.
168 // toString(): void;
169 };
170
171 export function unstable_useOpaqueIdentifier(): OpaqueIdentifier;
172
173 /**
174 * this should be an internal type
175 */
176 interface MutableSource<T> {
177 _source: T;
178 }
179
180 export type MutableSourceSubscribe<T> = (source: T, callback: () => void) => () => void;
181
182 /**
183 * @param source A source could be anything as long as they can be subscribed to and have a "version".
184 * @param getVersion A function returns a value which will change whenever part of the source changes.
185 */
186 export function unstable_createMutableSource<T>(source: T, getVersion: () => any): MutableSource<T>;
187
188 /**
189 * useMutableSource() enables React components to safely and efficiently read from a mutable external source in Concurrent Mode.
190 * The API will detect mutations that occur during a render to avoid tearing
191 * and it will automatically schedule updates when the source is mutated.
192 * @param MutableSource
193 * @param getSnapshot
194 * @param subscribe
195 *
196 * @see https://github.com/reactjs/rfcs/blob/master/text/0147-use-mutable-source.md
197 */
198 export function unstable_useMutableSource<T, TResult extends unknown>(MutableSource: MutableSource<T>, getSnapshot: (source: T) => TResult, subscribe: MutableSourceSubscribe<T>): TResult;
199}